Import Interface in Angular 2

In a Meteor application that uses Angular 2, I want to create a custom data type, something like this:

interface MyCustomType { index: number; value: string; } 

Then I want to use this custom type in multiple files. I tried to create a separate file called "mycustom.type.ts" with the following contents:

 export interface MyCustomType { index: number; value: string; } 

Then I try to import this type so that it can be used in another file:

 import MyCustomType from "./mycustom.type" 

However, Atom reports the following error:

 TS Error File '/.../mycustom.type.ts' is not a module ... 

How can I declare and import types so that they can be used in several places?

+7
interface angular typescript
source share
4 answers

You should import it like this:

 import { MyCustomType } from './mycustom.type'; 

do not forget { and } .

+13
source share

I am adding this answer because the accepted is incomplete. You have two problems:

First, you need to add export to your interface so that you can import it as a module:

export interface MyCustomType { index: number; value: string; }

Secondly, you need to add curly braces { } to the import statement:

import { MyCustomType } from './mycustom.type';

+7
source share

the problem is that you are inside the try change./with../ component

+2
source share

I think the problem is in the way:

Please refer to the example below:

If your file in another folder is listed below:

 import { IPosts } from "../interfaces/iposts.type"; 

iposts.type.ts:

 export interface IPosts { userId: number; id: number; title: string; body: string; } 
0
source share

All Articles