TypeScript 用于推断接口(interface)实现类中的类型参数

标签 typescript

它会提示arg的类型是any

interface Foo { 
  foo(arg: string): void 
}

class FooImpl implements Foo {
  foo(arg) { console.log(arg) }
}

我当然可以指定它是字符串,但它是重复的。

也许有一种方法可以调整 TypeScript 以使其自动推断出它是字符串

最佳答案

Maybe there's a way to tune TypeScript to make it infer that it's string automatically

没有。以下是相关的 TypeScript 问题及其不存在的原因:https://github.com/Microsoft/TypeScript/issues/1373

关于TypeScript 用于推断接口(interface)实现类中的类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58422516/

相关文章:

Javascript:根据多个值过滤对象数组

typescript - 之间有什么区别? : and | undefined in TypeScript?

javascript - TypeScript 中子类原型(prototype)的构造函数是可枚举的

Typescript 模块扩充与多个文件中的模块

javascript - Mongoose : "TypeError: "监听器“参数必须是一个函数”连接

node.js - 如何在 Visual Studio 2015 Community 中使用 tsconfig.json 文件?

typescript - Firestore批量深度合并

angular - 如何将 Angular Material 扩展面板箭头图标定位在左侧

Typescript 接口(interface)、函数和命名空间都具有相同的名称。哪个正在导出?

Angular 5 Material Snackbar 面板类配置