我很难过.以下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.
Run Code Online (Sandbox Code Playgroud)
但是,如果我重新命名 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;
};
Run Code Online (Sandbox Code Playgroud)
我正在使用TypeScript ^ 2.3.4
这是一个包含失败和非失败代码的完整示例:https://gist.github.com/iffy/9d518d78d6ead2fe1fbc9b0a4ba1a31d
该名称Account不是保留字,但定义为以下部分lib.d.ts:
/////////////////////////////
/// IE DOM APIs
/////////////////////////////
interface Account {
rpDisplayName?: string;
displayName?: string;
id?: string;
name?: string;
imageURL?: string;
}
Run Code Online (Sandbox Code Playgroud)
TypeScript 对您和您的那个执行声明合并,这会导致您遇到的问题.如果将文件转换为模块,则将特定于模块,TypeScript将停止尝试将其与全局模块相结合.Accountlib.d.tsAccount
例如,通过添加,export {};您可以轻松地将文件转换为模块:
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;
};
export {};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
468 次 |
| 最近记录: |