[英]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.