【问题标题】:Is `Account` a reserved word in TypeScript?`Account` 是 TypeScript 中的保留字吗?
【发布时间】: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


    【解决方案1】:

    名称Account不是保留字而是defined as part of lib.d.ts

    /////////////////////////////
    /// IE DOM APIs
    /////////////////////////////
    
    interface Account {
        rpDisplayName?: string;
        displayName?: string;
        id?: string;
        name?: string;
        imageURL?: string;
    }
    

    TypeScript 在您的 Accountlib.d.ts 中执行 declaration merging,这会导致您遇到问题。如果您将文件转换为模块,您的 Account 将特定于您的模块,TypeScript 将停止尝试将其与全局文件结合。

    例如,通过添加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 {};
    

    【讨论】:

    • 不客气。我以前被这个咬过。就我而言,它是ElementNode
    【解决方案2】:

    Typescript 保留字列表:

    保留字:

    以下关键字是保留的,不能用作标识符:

    break             case              catch                 class  
    const             continue          debugger              default  
    delete            do                else                  enum  
    export            extends           false                 finally  
    for               function          if                    import  
    in                instanceof        new                   null  
    return            super             switch                this  
    throw             true              try                   typeof  
    var               void              while                 with
    

    以下关键字不能用作严格模式代码中的标识符,否则不受限制:

    implements            interface         let                  package  
    private               protected         public               static  
    yield
    

    以下关键字不能用作用户定义的类型名称,但不受其他限制:

    any               boolean           number            string  
    symbol
    

    以下关键字在某些上下文中具有特殊含义,但都是有效的标识符:

    abstract             as                    async                 await  
    constructor          declare               from                  get  
    is                   module                namespace             of  
    require              set                   type
    

    【讨论】:

      猜你喜欢
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 2016-08-19
      • 1970-01-01
      • 2020-05-14
      相关资源
      最近更新 更多