lorefnon
lorefnon

Reputation: 13105

Associating npm package with local typescript definitions file

I am using an npm package (say foo) and during development I would like to associate this package with a local (committed along with project files) typescript definitions file. I explicitly do not intend to publish the definitions file as an npm package because as of now it is incomplete and covers only the parts of the library I actually use.

So far I have tried the following:

Adding the following to tsconfig.json

"typeRoots": [
  "./node_modules/@types",
  "./src/types"
],

And added definition file in ./src/types/foo/index.d.ts. However neither VSCode nor the typescript webpack-loader are able to associate imports from 'foo' with the exports in the definition file.

What is the right approach for handling this?

Upvotes: 1

Views: 222

Answers (2)

lorefnon
lorefnon

Reputation: 13105

Adding a reference to the type definition in "files" section of tsconfig.json fixes the issue:

{
    "compilerOptions": { ... },
    "files" : ["./src/types/foo.d.ts"]
}

This solution proposed by Dan works too, but requires more work - if one intends to publish the definitions eventually that is the ideal approach.

Upvotes: 1

Dan
Dan

Reputation: 2858

That's pretty much what I did here

created "my Type" for an existing lib following the instructions from @types
and publish it to "my" Github. instead of publishing to @types

finally

npm i git+https://github.com/my/repo.git

it's going to end up in node_modules/@types/chosen-name
exactly where tsc expect it to be

I didn't try but I don't see a reason why it shouldn't work from other sources

npm i ../myrepo

Upvotes: 1

Related Questions