实现 Typescript 类接口不保留方法类型

Dan*_*ott 7 typescript

我正在尝试编写一个 Typescript 接口,然后编写一个实现该接口的类。

\n\n

问题是我似乎无法从接口获取方法签名以应用于该类。

\n\n

这是一个简化的示例:

\n\n
export interface Foo {\n  bar(value: string): void;\n}\n\n\nexport class MyFoo implements Foo {\n  // \xe2\x9c\x94\xef\xb8\x8f Typescript error:\n  // Property \'bar\' is missing in type \'MyFoo\' but required in type \'Foo\'\n}\n\n\nexport class MyFoo implements Foo {\n  // value is inferred as `any` instead of `string`, \n  // and there aren\'t any errors with the return type mismatch\n  bar(value) { return true; }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

编译器似乎知道该bar方法应该存在,但由于某种原因不保留它的签名。

\n

bas*_*rat 8

似乎编译器知道 bar 方法应该存在,但由于某种原因不保留它的签名。

正确的。原因如下: https: //github.com/Microsoft/TypeScript/pull/6118#issuecomment-216595207

因此,开发人员有责任添加他们需要的注释。