19

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?

5 Answers 5

27

You should rather import it like that :

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

don't forget the { and }.

3
  • I have the same issue but I already had the brackets { } so this didn't work for me. Any other possible solutions? Commented Apr 3, 2017 at 10:07
  • 1
    This answer is only part of the issue. If you want to import a module you need to set the interface as exportable - .e.g. export interface MyCustomType {
    – edzillion
    Commented Jun 25, 2017 at 13:40
  • If you want to import you have to export. Of course. Plus, OP already exported it so I really don't see your point here.
    – maxime1992
    Commented Jun 26, 2017 at 14:11
18

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';

2
  • What is the purpose of the curly brackets { }? Commented Sep 1, 2017 at 16:39
  • 2
    @alibenmessaoud for the import it describes which modules to import from the file. e.g. import { ModuleA, ModuleB, ModuleC } from './modulefile'
    – edzillion
    Commented Sep 1, 2017 at 17:25
2

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

1

I tried all of top answers but still i get the same error then I understand moreover @edzillion issues one issue can be raise this error .

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

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

Third: your interface name must be match your file name(file that interface exist in it).

0

I think, problem is in the path:

Kindly refer below example:

If your file in another folder then refer below:

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

iposts.type.ts :

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.