TypeScript declares that the file is published

May 07, 2021 23:00 TypeScript

Table of contents

TypeScript declares that the file is published

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:

  1. Bundle with your npm package, or
  2. The website published to @types 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.

Contains a declaration file to your npm package

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.

Depend on

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": [
        "[email protected]",
        "@types/[email protected]",
        "[email protected]"

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

Red flags

/// <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.

Package the claims on which you depend

If your type declaration depends on another package:

  • Don't put dependent packages in your bag and keep them in your files.
  • Do not copy claims into your package.
  • You should rely on the npm type declaration package if the dependency package does not contain its own declaration.

Publish your statement documents

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.

Publish to @types

@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.