4

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?

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

7 Comments

how can I properly import this interface? import {String} from "../validators/validators"; as no exported member String if I do import * as S from "../validators/validators"; I don't see the String interface available through intellisense.
If you don't need the wrapping Validator namespace, just get rid of it by removing the first and last line from my code snippet.
I'm most interested in exporting multiple interfaces in the same file. is there a good way to export multiple interfaces from the same file?
Sure, just add multiple exports in the same file! export interface A {...} export interface B {...} You don't really need namespaces when you're going to use ES6 modules.
thanks for the heads up. I'm unsure when VS Code has a bug and when I've gotten the wrong TypeScript syntax. +1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.