繁体   English   中英

带有签名的打字稿箭头函数接受错误的参数

[英]Typescript arrow function with signature accepts wrong parameter

考虑类型:

type GenericFunction = <T>(props: Array<T>) => void

和箭头函数:

const test: GenericFunction = <X>(props: X) => {
let dd: X }

尽管GenericFunction类型将Array<T>定义为参数,但test函数为什么接受X作为参数?

X某些类型将满足GenericFunction的签名,这就是编译器不会给出错误的原因。 现在X必须是数组类型,这只是一个隐式约束。

仅当您为X添加与此隐式约束冲突的约束时,您才会收到错误:

// this gives a compile error
const test: GenericFunction = <X extends number>(props: X) => {};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM