【发布时间】:2020-02-03 22:56:34
【问题描述】:
在 typescript 中使用组合方法而不是继承之一时,我想根据实体“可以”而不是“是”来描述我的实体。为了做到这一点,我需要创建一些复杂的接口,然后为我的类(我使用类是为了不创建手动原型链并且不破坏我认为存在于 js 引擎中的一些优化)来实现我的接口。但是,当没有正确推断出方法的类型时,这会导致奇怪的行为。相反,当使用对象并将它们声明为相同的接口类型时,一切都按预期工作。
所以我正在使用带有 typescript 3.6.3 的 VSCode。我已经为 2d 形状创建了接口,该接口应该具有将所有法线返回到边缘的方法。然后我创建了实现该接口的类,我希望它需要这个方法,它应该具有相同的返回类型(这部分有效)和相同的参数类型(这个没有)。参数被推断为任何。我的问题是我不想仅仅为了获得一致的 VSCode 行为而手动创建原型链。
另外,当在控制台中运行 tsc 时,我得到相同的错误,因为参数在类方法中是“任何”类型,而在访问不存在的 prop 时对象方法内部出现预期错误
interface _IVector2 {
x: number;
y: number;
}
interface _IShape2D {
getNormals: ( v: string ) => _IVector2[];
}
export class Shape2D implements _IShape2D {
getNormals( v ) {
console.log( v.g );
^ ----- no error here
return [{} as _IVector2];
}
}
export const Test: _IShape2D = {
getNormals( v ) {
console.log( v.g );
^------ here we get expected error that
^------ 'g doesn`t exist on type string'
return [{} as _IVector2];
}
};
我的 tsconfig.json
{
"compilerOptions": {
"target": "es2017",
"allowSyntheticDefaultImports": true,
"checkJs": false,
"allowJs": true,
"noEmit": true,
"baseUrl": ".",
"moduleResolution": "node",
"strict": true,
"strictNullChecks": true,
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noFallthroughCasesInSwitch": true,
"jsx": "react",
"module": "commonjs",
"alwaysStrict": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"noErrorTruncation": true,
"removeComments": true,
"resolveJsonModule": true,
"sourceMap": true,
"watch": true,
"skipLibCheck": true,
"paths": {
"@s/*": ["./src/*"],
"@i/*": ["./src/internal/*"]
}
},
"exclude": [
"node_modules"
]
}
预期:
- 类方法的参数应该被推断为字符串
实际:
- 方法的参数被推断为any
最后我的问题如下: “这种行为在 ts 中是无法实现的吗?我应该求助于手写(哦,亲爱的……)原型链和原型的简单对象?”
提前谢谢你!
【问题讨论】:
-
一个方法的签名由它的名字和参数组成,所以打字稿只有在你指定相同的参数类型(在你的情况下是字符串)时才会知道你正在从接口实现方法。跨度>
-
它实际上可以正确推断返回类型,即使我没有指定任何参数。那么根据这个能力,为什么它不“理解”应该是什么参数类型呢?
标签: typescript class methods interface implements