Is `Account` a reserved word in TypeScript?
I'm stumped. The following TypeScript code will fail to compile with this error:
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.
But if I rename class Account
to class Hello
, it doesn't work. I've gone crazy? Does anyone else see the same behavior?
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;
};
I am using TypeScript ^ 2.3.4
Here is a complete example with unsuccessful and unreproducible code: https://gist.github.com/iffy/9d518d78d6ead2fe1fbc9b0a4ba1a31d
source to share
The name is Account
not a reserved word, but it is defined as part oflib.d.ts
:
/////////////////////////////
/// IE DOM APIs
/////////////////////////////
interface Account {
rpDisplayName?: string;
displayName?: string;
id?: string;
name?: string;
imageURL?: string;
}
TypeScript is merging declarations on yours Account
and ons lib.d.ts
, which is causing the problem. If you turn your file into a module, yours Account
will be specific to your module, and TypeScript will stop trying to combine it with the global one.
For example, by adding export {};
, you can trivially turn your file into a module:
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 {};
source to share