Mega Man Mega Man - 4 months ago 48
Javascript Question

Typescript 2.0 Namespaces file is not a module

I'm using TypeScript 2.1.0 and VS Code 1.3 and I'm trying to get used to the module/namespace system in TypeScript 2. I have a file

validators.ts


namespace Validator{
export interface String{
isValid(s:string):boolean;
}
}
declare module 'validator'{
export = Validator;
}


And when I try to use it like
import {Validator} from "../validators/validators";


I get the error
validators.ts is not a module
.

I'm installing my external types with
npm i @types/myTypes
and after installing bluebird and copying its structure I'm still seeing this error. Is this a VS Code bug, or a me bug?

Answer

It's much simpler:

export namespace Validator{
  export interface String{
    isValid(s:string):boolean;
  }
}

You should use declare module only when writing type definitions for external libraries. For your own TypeScript code, use regular ES6 style import and export syntax.

Comments