我有以下 typescript :
let fn = function (): boolean {
return true
}
let res1: number
res1 = fn.call(null) // Should error
我不应该被允许分配 res1
因为它被声明为 number
而 fn.call()
的结果将是 bool 值
。打卡the playground .
最佳答案
有一个特殊的编译器选项(在 playground 中不可用)使 call
和 bind
和 apply
保留正确的类型。
选项是strictBindCallApply
,你可以阅读它here :
Enable stricter checking of the bind, call, and apply methods on functions.
将此选项设置为 true,您的代码将按预期出错。
关于typescript - call() 没有返回正确的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58201293/