POV
POV

Reputation: 12005

How to import module correct in Typescript?

I have a declared module:

declare module conflicts {

  export interface Item {}

  export interface Item2 {}

  export interface Item3 {}

}

I tried ti import this module in component like:

import * from '../../../_models/conflicts/conflicts';

Then use it:

let c = {} as conflicts.item3;

But it does not work

Upvotes: 0

Views: 58

Answers (1)

Blake Simmon
Blake Simmon

Reputation: 36

The module declaration should look like the following:

export declare module Conflicts {

  export interface Item {}

  export interface Item2 {}

  export interface Item3 {}
}

The import should be:

import * as conflicts from '../../../_models/conflicts/conflicts';

Then, to use the import, do this:

let c = {} as conflicts.Conflicts.Item3;

Notes:

  • conflicts with a lower-case 'c' in the usage is basically the contents of the import.
  • Conflicts with a capital 'C' in the usage is the module itself.
  • Be sure you capitalize the 'I' of 'Item3' to match the interface declaration in the module.
  • In the as conflicts part of the import, you can really change conflicts to anything you want. This is just for setting how the import will be referred to in the rest of the file.

Upvotes: 2

Related Questions