Skip to content
tezvyn:
Menu

Shipping TypeScript Types in package.json

Source: typescriptlang.orgMediumHow cards are made

The types field in package.json tells TypeScript where to find your package's type definitions. Use it when publishing a library to enable autocompletion for users. The footgun is putting type dependencies in devDependencies instead of dependencies.

Why it exists

JavaScript packages use the main field in package.json to tell runtimes like Node.js where the entry-point file is. When TypeScript became popular, it needed a similar, parallel mechanism to find the type definitions for a package, enabling editor autocompletion and type-checking across project boundaries.

The mental model

Think of package.json as a package's public interface. The main field says, "Here's my entry point for runtime execution." The types field adds, "And here's my corresponding type declaration file for compile-time analysis and editor support." They are parallel pointers for two different consumers: the JavaScript runtime and the TypeScript compiler.

How it works

In your package.json, you add a types property (or its older synonym, typings). Its value is a path to your main declaration file, which typically ends in .d.ts. When another project imports your package, the TypeScript compiler looks at this field to find and load your types, making them available for type checking. If your package is consumed, its own type dependencies must be available, which leads to a common mistake.

When to use it

Use the types field whenever you publish an npm package written in TypeScript, or a JavaScript package for which you are providing your own type definitions. This is the standard for bundling types with your code, making your package immediately useful in a TypeScript environment.

When not to use it

If your package is a pure application not intended for use as a library, or if it's an older JavaScript project whose types are exclusively managed by the community on DefinitelyTyped (@types/some-package), you would not include this field. The presence of the types field signals that you are the authority for your package's types.

One canonical example

A library's package.json might look like this: { "name": "my-awesome-lib", "main": "./dist/index.js", "types": "./dist/index.d.ts" }. A consumer of this library gets runtime code from index.js, but their editor and the TypeScript compiler get type information from index.d.ts. If my-awesome-lib's types depend on @types/node, it must list that in its dependencies, not devDependencies, so the consumer's compiler can find it.

Interview question

When publishing a TypeScript library with a "types" field, what is crucial for its type dependencies?

  • a.They must be bundled directly into the output .d.ts files.
  • b.They must be declared in dependencies for consumer type resolution.Correct
  • c.The TypeScript compiler automatically resolves them without explicit declaration.
  • d.They should be listed in devDependencies to prevent runtime conflicts.
Why?

The card states that a package's type dependencies must be listed in its 'dependencies' so that the consumer's TypeScript compiler can correctly find and resolve them. Listing them in 'devDependencies' is a common mistake, as these are not available to consumers.

Just read this? Test yourself on what you have been reading.

Read the original → typescriptlang.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on typescript — each one lists the topics its interview covers.

See open roles