May 07, 2021 TypeScript
Now that we've followed the steps in the guide to write a statement file, it's time to publish it to npm. There are two main ways to publish a declaration file to npm:
If you can control the npm package that you want to use the declaration file you publish, the first method is recommended. In this case, your claim file is always delivered with JavaScript.
If your package has a master
.js
you still need to specify the master declaration file in
package.json
S
et
types
property to point to the bundled claims file.
Like what:
{
"name": "awesome",
"author": "Vandelay Industries",
"version": "1.0.0",
"main": "./lib/main.js",
"types": "./lib/main.d.ts"
}
Note
"typings"
has
"types"
and can also be used.
It is also important to note that if the main declaration file
index.d.ts
location is in the root of the package
index.js
you do not need
"types"
property.
All dependencies are managed by npm.
Make sure that the claims packages you rely on are indicated in
package.json
"dependencies"
for example, assuming we write a package that relies on Browseify and TypeScript.
{
"name": "browserify-typescript-extension",
"author": "Vandelay Industries",
"version": "1.0.0",
"main": "./lib/main.js",
"types": "./lib/main.d.ts",
"dependencies": [
"browserify@latest",
"@types/browserify@latest",
"typescript@next"
]
}
Here, our packages rely
browserify
and
typescript
browserify
doesn't bundle its claims file in its npm package, so we
@types/browserify
to get its claims file.
typescript
places the declaration file in the npm package, so we don't have to rely on additional packages.
Our packages expose claims files from both packages, so
browserify-typescript-extension
need these dependencies as well. T
herefore, we use
"dependencies"
"devDependencies"
users will need to manually install those packages.
If we're just writing a command-line app and our packages won't be used as a library, then
devDependencies
/// <reference path="..." />
Do
not use
/// <reference path="..." />
/// <reference path="../typescript/lib/typescriptServices.d.ts" />
....
You
should
/// <reference types="..." />
/// <reference types="typescript" />
....
Be sure to read the section on "Using Dependencies" (./Library Structures.md?users-dependencies) to learn more.
If your type declaration depends on another package:
After you publish the claims package, be sure to add a reference to the DefinitelyTyped external package list. This lets the finder know that your package provides its own claims file.
@types the following package is automatically published from DefinitelyTyped, via the types-publisher tool. I f you want your package to @types as a package, submit a pull request to https://github.com/DefinitelyTyped/DefinitelyTyped. See the details of the contribution guidelines page here.