typescript - call() 没有返回正确的类型

标签 typescript

我有以下 typescript :

let fn = function (): boolean {
    return true
}

let res1: number

res1 = fn.call(null) // Should error

我不应该被允许分配 res1 因为它被声明为 numberfn.call() 的结果将是 bool 值。打卡the playground .

这是一个 issue但它是 fixed ,那我是不是做错了什么?

最佳答案

有一个特殊的编译器选项(在 playground 中不可用)使 callbindapply 保留正确的类型。

选项是strictBindCallApply,你可以阅读它here :

Enable stricter checking of the bind, call, and apply methods on functions.

将此选项设置为 true,您的代码将按预期出错。

关于typescript - call() 没有返回正确的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58201293/

相关文章:

angular - IONIC 中没有 MapsAPILoader 的提供者

javascript - 获取类型错误 : Cannot set property '0' of undefined when trying to add to array

angular - 我什么时候应该使用 TS 在 Angular 2+ 中导入类型?

javascript - Google Maps API v3 TypeScript 定义文件 bool 而不是 Boolean

typescript - 为什么 TypeScript 中没有重载构造函数实现?

angular - 将附加参数传递给 Angular 组件内的方法装饰器

typescript - 在类中使用 requestAnimationFrame

types - 是否可以在 TypeScript 的类中定义类型(字符串文字联合)?

reactjs - 何时在带有 React 的 Typescript 中使用私有(private)/ protected 方法

reactjs - Emotion.js 与 create-react-app : ReferenceError: jsx is not defined