0

I have a TypeScript class file:

class SomeClass {
  public load(): void {
    console.log('loaded');
  }
}

And another ts file, which are mostly functions, that wants to use this class:

// declare it
declare var SomeClass;

function useIt() {
  const c = new SomeClass();
  c.load();
}

In Visual Studio 2019, it tells me that SomeClass is duplicate (e.g. at the declare var SomeClass line). Still generates the .js, but there is also an error.

So I understand that it's a duplicate declaration, so with that in mind, how do I tell VS to compile the .ts files in isolation from each other?

1
  • I'm not sure to understand, why do you re-declare the class ? Why don't you import it ? Commented Mar 20, 2020 at 17:12

1 Answer 1

1

If you are trying to use the SomeClass class in other ts file, I believe that the correct approach you should take is exporting the class in the file you defined it, and importing the class in the ts files you need to use it. See the example below.

some-class.ts:

export class SomeClass {
    public load(): void {
        console.log("loaded");
    }
}

func.ts:

import { SomeClass } from './some-class';

function useIt() {
  const c = new SomeClass();
  c.load();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.