0

Here is a very simple typescript code in a file 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();

I used npx tsc test.ts to compile the above, and I got these error messages

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

What is wrong with the code? Where does the duplicate come from?

Anthony Kong
  • 37,791
  • 46
  • 172
  • 304

1 Answers1

2

const { config: A } = func();

This line is saying you expect func() to return an object with a config property, and you're trying to assign the value of that to a new const named A, but you've already defined A as a class

Greg Hornby
  • 7,443
  • 1
  • 18
  • 17
  • Thx! I thought I can use `:A` there is to specify the data type of the variable `config`. So how can I restrict the type of `config`? – Anthony Kong Oct 26 '18 at 02:46
  • 1
    config should be automatically implied. `const { config } = func();` should automatically make config of type A. But if you want explicit state the type, you'd have to do: let config: A; ({ config } = func()); – Greg Hornby Oct 26 '18 at 02:50
  • Awesome! It's exactly what I am looking for! – Anthony Kong Oct 26 '18 at 03:00