代码之家  ›  专栏  ›  技术社区  ›  Anthony Kong

错误TS2300:标识符“A”重复。但是只有一个类声明

  •  0
  • Anthony Kong  · 技术社区  · 6 年前

    下面是一个文件中非常简单的typescript代码 test.ts

    // Testing
    //
    class A {
       private field1;
       config;
        constructor() {
                this.field1 = undefined;
                this.config = undefined;
        }
    };
    
    function func() {
        const config = new A();
        return { config };
    };
    
    const { config: A } = func();
    

    npx tsc test.ts 我得到了这些错误信息

    test.ts(3,7): error TS2300: Duplicate identifier 'A'.
    test.ts(17,17): error TS2300: Duplicate identifier 'A'.
    

    代码有什么问题?复制品从哪里来?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Greg Hornby    6 年前

    const { config: A } = func();

    这句话是说你期望 func() 返回带有 config 属性,并尝试将其值赋给名为 A ,但你已经定义了 A. 作为一个班级