bravely
bravely

Reputation: 185

In Typescript 2, how can I use a local definition file?

I'm just trying to get Typescript 2 to use a local definition file provided by me, for a javascript library I installed through NPM.

First, I installed MockDate:

npm i --save-dev mockdate

Then, I attempted to import it via

import MockDate from 'mockdate'

Only to be told it couldn't find it. From here, I've created local definitions, I've declare module 'mockdate', I've done

declare module MockDate {
  function set(date: Date): void
}

at the top of the file- and no matter what I do, I continue to receive(from atom-typescript) "Cannot find module 'mockdate'".

Upvotes: 4

Views: 876

Answers (2)

daniel
daniel

Reputation: 11

  1. write a .d.ts file
  2. import .d.ts file to your project with Triple-Slash Directives

Triple-Slash Directives code sample

  1. then you can get your type info in the project

Upvotes: 1

AlexG
AlexG

Reputation: 4035

Almost there! You need to write:

declare module 'mockdate' {
  function set(date: Date): void
}

And make sure this d.ts file is picked up by your tsconfig.

Upvotes: 3

Related Questions