Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import interface in Angular 2

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

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

I then want to use this custom type in multiple files. I've tried creating a separate file named "mycustom.type.ts", with the following content:

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

I then attempt 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 should I be declaring and importing types, so that they can be used in multiple places?

like image 758
James Newton Avatar asked Nov 21 '16 12:11

James Newton


2 Answers

You should rather import it like that :

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

don't forget the { and }.

like image 66
maxime1992 Avatar answered Oct 09 '22 10:10

maxime1992


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

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

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

Second you need to add the curly brackets { } to the import statement:

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

like image 37
edzillion Avatar answered Oct 09 '22 09:10

edzillion