【发布时间】:2017-06-15 16:56:15
【问题描述】:
我被难住了。以下 TypeScript 代码无法编译并出现此错误:
fails.ts(10,7): error TS2420: Class 'Account' incorrectly implements interface 'IAccount'.
Property 'name' is optional in type 'Account' but required in type 'IAccount'.
fails.ts(11,3): error TS2403: Subsequent variable declarations must have the same type. Variable 'id' must be of type 'string', but here has type 'number'.
fails.ts(11,3): error TS2687: All declarations of 'id' must have identical modifiers.
fails.ts(14,3): error TS2687: All declarations of 'name' must have identical modifiers.
但如果我将class Account重命名为class Hello,它不会失败。我要疯了吗?有没有其他人看到相同的行为?
interface IObject {
id: number;
table_name: string;
};
interface IAccount extends IObject {
user_id: number;
name: string;
};
class Account implements IAccount {
id: number;
table_name: string = 'account';
user_id: number;
name: string;
};
我正在使用 TypeScript ^2.3.4
这是一个包含失败和非失败代码的完整示例:https://gist.github.com/iffy/9d518d78d6ead2fe1fbc9b0a4ba1a31d
【问题讨论】:
标签: typescript