user11081980
user11081980

Reputation: 3289

How to "import" a namespace in a TypeScript Definition file

The moment.js type definition is declared within a namespace:

declare namespace moment {
  interface Moment {
...

In order to declare an object of type Moment, I do:

let myMoment: moment.Moment;

My question is - is there a way to "import" the moment namespace so I can avoid repeating every time? Very much like C#.

Upvotes: 3

Views: 1327

Answers (2)

Sayan Pal
Sayan Pal

Reputation: 4956

If you can use ES6 style imports then you can simply do something like following.

import { Moment, Duration, OrAnyOtherExportedMemberFromMoment } from "moment";
let myMoment: Moment;
let myDuration: Duration;

In case you need this in your type definition files from you source file:

With compilerOptions.declaration set to true in your tsconfig.json, and using gulp-typescript (search for tsResult.dts.pipe), you can generate type definitions from your source files.

Hope this helps.

Upvotes: 0

John Montgomery
John Montgomery

Reputation: 7096

I don't know if there's a way to "import" an entire namespace, but you can do it on an item-by-item basis like this:

type Moment = moment.Moment

Upvotes: 3

Related Questions