According to this article typings system for typescript 2.0 has changed and so it is not clear how to attach custom typings now. Should I always create NPM package for that?
Thank you in advance!
According to this article typings system for typescript 2.0 has changed and so it is not clear how to attach custom typings now. Should I always create NPM package for that?
Thank you in advance!
You can create local custom typings just for your project, where you can declare types for JS libraries. For that, you need to:
Create directory structure to keep your type declaration files so that your directory structure looks similar to this:
.
├── custom_typings
│ └── some-js-lib
│ └── index.d.ts
└── tsconfig.json
In the index.d.ts
file, add a declaration for your JS library:
declare module 'some-js-lib' {
export function hello(world: string): void
}
(Optional: skip if you have TypeScript >= 4.x) Add a reference to this type declaration in the compilerOptions
section of your tsconfig.json
:
{
"compilerOptions": {
...
"typeRoots": ["./node_modules/@types", "./custom_typings"]
},
...
}
Use the declared module in your code:
import { hello } from 'some-js-lib'
hello('world!')
index.d.ts
or declarations.d.ts
can go anywhere in the project. –
Hospitaler declare module ...
into any .d.ts
file. –
Cleghorn node_modules/@types
– at least not with TS 2.1. –
Cleghorn node_modules/@types
back. I've updated the answer, thanks! –
Cleghorn import Worker from './worker.ts'
–
Toxicology tsconfig.json
. In fact, specifying typeRoots
in step 3 doesn't seem to be necessary anymore 🤔 –
Cleghorn © 2022 - 2024 — McMap. All rights reserved.