`.Or if there are more people, it can be multiline
- ```typescript
- // Definitions by: Alice
- // Bob
- // Steve
- // John
- ```
Once a week the Definition Owners are synced to the file
.github/CODEOWNERS which is our source of truth.
FAQ
What exactly is the relationship between this repository and the @types packages on npm?
I've submitted a pull request. How long until it is merged?
It depends, but most pull requests will be merged within a week.
Some PRs can be merged by the owners of a module, and they can be merged much faster.
Roughly:
PRs which only change the types of a module, and have corresponding tests changes will be merged much faster
PRs that have been approved by an author listed in the definition's header are usually merged more quickly; PRs for new definitions will take more time as they require more review from maintainers. Each PR is reviewed by a TypeScript or Definitely Typed team member before being merged, so please be patient as human factors may cause delays. Check the
New Pull Request Status Board to see progress as maintainers work through the open PRs.
I'd like to submit a change to a very popular project, why are they treated differently?
For changes to very popular modules, e.g. Node/Express/Jest which have many millions of downloads each per week on npm, the requirements for contributions are a bit higher.
Changes to these projects can have massive ecosystem effects, and so we treat changes to them with a lot of care.
These modules require both a sign-off from a DT maintainer, and enthusiastic support from the module owners. The bar for passing this can be quite high, and often PRs can go stale because it doesn't have a champion.
If you're finding that no-one is committing, try to make your PR have a smaller focus.
My PR is merged; when will the @types npm package be updated?
#### I'm writing a definition that depends on another definition. Should I use `` or an import?
If the module you're referencing is an external module (uses export), use an import.
If the module you're referencing is an ambient module (uses `declare module`, or just declares globals), use ``.
Some packages have no tslint.json, and some tsconfig.json are missing "noImplicitAny": true, "noImplicitThis": true, or "strictNullChecks": true.
Then they are wrong, and we've not noticed yet. You can help by submitting a pull request to fix them.
Can I change/enforce formatting settings for modules?
No. We've explored trying to make DT's code-formatting consistent before but reached an impasse due to the high activity on the repo. We include formatting settings via a [.editorconfig](.editorconfig) and [.prettierrc.json](.prettierrc.json). These are exclusively for tooling in your editor, their settings don't conflict and we don't plan on changing them. Nor do we plan on enforcing a specific style in the repo. We want to keep the barriers to contributions low.
Can I request a definition?
What about type definitions for the DOM?
If types are part of a web standard, they should be contributed to
TypeScript-DOM-lib-generator so that they can become part of the default
lib.dom.d.ts.
Should I add an empty namespace to a package that doesn't export a module to use ES6 style imports?
Some packages, like
chai-http, export a function.
Importing this module with an ES6 style import in the form import * as foo from "foo"; leads to the error:
error TS2497: Module 'foo' resolves to a non-module entity and cannot be imported using this construct
This error can be suppressed by merging the function declaration with an empty namespace of the same name, but this practice is discouraged.
It is more appropriate to import the module using the import foo = require("foo"); syntax.
Nevertheless, if you want to use a default import like import foo from "foo"; you have two options:
- you can use the [--allowSyntheticDefaultImports compiler option](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-8.html#support-for-default-import-interop-with-systemjs) if your module runtime supports an interop scheme for non-ECMAScript modules, i.e. if default imports work in your environment (e.g. Webpack, SystemJS, esm).
- you can use the [--esModuleInterop compiler option](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#support-for-import-d-from-cjs-form-commonjs-modules-with---esmoduleinterop) if you want TypeScript to take care of non-ECMAScript interop (since TypeScript 2.7).
A package uses export =, but I prefer to use default imports. Can I change export = to export default?
Like in the previous question, refer to using either the [--allowSyntheticDefaultImports](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-8.html#support-for-default-import-interop-with-systemjs)
or [--esModuleInterop](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#support-for-import-d-from-cjs-form-commonjs-modules-with---esmoduleinterop)
compiler options.
Do not change the type definition if it is accurate.
For an npm package, export = is accurate if node -p 'require("foo")' works to import a module, and export default is accurate if node -p 'require("foo").default' works to import a module.
I want to use features from very new TypeScript versions.
Then you will have to add a comment to the last line of your definition header (after // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped): // Minimum TypeScript Version: X.Y. This will set the lowest minimum supported version.
However, if your project needs to maintain types that are compatible with, say, 3.7 and above at the same time as types that are compatible with 3.6 or below, you will need to use the typesVersions feature.
Here's a short example to get you started:
1. You'll have to add a package.json file to your package definition, with the following contents:
- ``` json
- {
- "private": true,
- "types": "index",
- "typesVersions": {
- "<=3.6": { "*": ["ts3.6/*"] }
- }
- }
- ```
2. Create the sub-directory mentioned in the typesVersions field inside your types directory (ts3.6/ in this example).
ts3.6/ will support TypeScript versions 3.6 and below, so copy the existing types and tests there.
You'll need to delete the definition header from ts3.6/index.d.ts since only the root index.d.ts is supposed to have it.
3. Set the baseUrl and typeRoots options in ts3.6/tsconfig.json to the correct paths, which should look something like this:
- ``` json
- {
- "compilerOptions": {
- "baseUrl": "../../",
- "typeRoots": ["../../"]
- }
- }
- ```
4. Back in the root of the package, add the TypeScript 3.7 features you want to use.
When people install the package, TypeScript 3.6 and below will start from ts3.6/index.d.ts, whereas TypeScript 3.7 and above will start from index.d.ts.
You can look at
bluebird for an example.
I want to add a DOM API not present in TypeScript by default.
If the standard is still a draft, it belongs here.
Use a name beginning with dom- and include a link to the standard as the "Project" link in the header.
When it graduates draft mode, we may remove it from Definitely Typed and deprecate the associated @types package.
How do Definitely Typed package versions relate to versions of the corresponding library?
Each Definitely Typed package is versioned when published to npm.
The
DefinitelyTyped-tools (the tool that publishes
@types packages to npm) will set the declaration package's version by using the
major.minor version number listed in the first line of its
index.d.ts file.
- ``` js
- // Type definitions for Node.js 10.12
- // Project: https://nodejs.org/
- // Definitions by: Microsoft TypeScript
- // Definitely Typed
- // Alberto Schiabel
- ```
Because 10.12 is at the end of the first line, the npm version of the @types/node package will also be 10.12.x.
Note that the first-line comment in the index.d.ts file should only contain the major.minor version (e.g. 10.12) and should not contain a patch version (e.g. 10.12.4).
This is because only the major and minor release numbers are aligned between library packages and type declaration packages.
The patch release number of the type declaration package (e.g. .0 in 10.12.0) is initialized to zero by Definitely Typed and is incremented each time a new @types/node package is published to npm for the same major/minor version of the corresponding library.
Sometimes type declaration package versions and library package versions can get out of sync.
Below are a few common reasons why, in order of how much they inconvenience users of a library.
Only the last case is typically problematic.
As noted above, the patch version of the type declaration package is unrelated to the library patch version.
This allows Definitely Typed to safely update type declarations for the same major/minor version of a library.
If updating a package for new functionality, don't forget to update the version number to line up with that version of the library.
If users make sure versions correspond between JavaScript packages and their respective @types packages, then npm update should typically just work.
It's common for type declaration package updates to lag behind library updates because it's often library users, not maintainers, who update Definitely Typed when new library features are released.
So there may be a lag of days, weeks, or even months before a helpful community member sends a PR to update the type declaration package for a new library release.
If you're impacted by this, you can be the change you want to see in the world and you can be that helpful community member!
:exclamation: If you're updating type declarations for a library, always set the major.minor version in the first line of index.d.ts to match the library version that you're documenting! :exclamation:
If a library is updated to a new major version with breaking changes, how should I update its type declaration package?
Semantic versioning requires that versions with breaking changes must increment the major version number.
For example, a library that removes a publicly exported function after its 3.5.8 release must bump its version to 4.0.0 in its next release.
Furthermore, when the library's 4.0.0 release is out, its Definitely Typed type declaration package should also be updated to 4.0.0, including any breaking changes to the library's API.
Many libraries have a large installed base of developers (including maintainers of other packages using that library as a dependency) who won't move right away to a new version that has breaking changes, because it might be months until a maintainer has time to rewrite code to adapt to the new version.
In the meantime, users of old library versions still may want to update type declarations for older versions.
If you intend to continue updating the older version of a library's type declarations, you may create a new subfolder (e.g. /v2/) named for the current (soon to be "old") version, and copy existing files from the current version to it.
Because the root folder should always contain the type declarations for the latest ("new") version, you'll need to make a few changes to the files in your old-version subdirectory to ensure that relative path references point to the subdirectory, not the root.
1. Update the relative paths in tsconfig.json as well as tslint.json.
2. Add path mapping rules to ensure that tests are running against the intended version.
For example, the [history](https://github.com/ReactTraining/history/) library introduced breaking changes between version 2.x and 3.x.
Because many users still consumed the older 2.x version, a maintainer who wanted to update the type declarations for this library to 3.x added a v2 folder inside the history repository that contains type declarations for the older version.
At the time of writing, the [history v2 tsconfig.json](https://github.com/%44efinitelyTyped/DefinitelyTyped/blob/1253faabf5e0d2c5470db6ea87795d7f96fef7e2/types/history/v2/tsconfig.json) looks roughly like:
- ``` json
- {
- "compilerOptions": {
- "baseUrl": "../../",
- "typeRoots": ["../../"],
- "paths": {
- "history": [ "history/v2" ]
- }
- },
- "files": [
- "index.d.ts",
- "history-tests.ts"
- ]
- }
- ```
If there are other packages in Definitely Typed that are incompatible with the new version, you will need to add path mappings to the old version.
You will also need to do this recursively for packages depending on the old version.
For example, react-router depends on history@2, so [react-router tsconfig.json](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-router/v2/tsconfig.json) has a path mapping to "history": [ "history/v2" ].
Transitively, react-router-bootstrap (which depends on react-router) also needed to add the same path mapping ("history": [ "history/v2" ]) in its tsconfig.json until its react-router dependency was updated to the latest version.
Also, `/// ` will not work with path mapping, so dependencies must use `import`.
How do I write definitions for packages that can be used globally and as a module?
The TypeScript handbook contains excellent
general information about writing definitions, and also
this example definition file which shows how to create a definition using ES6-style module syntax, while also specifying objects made available to the global scope. This technique is demonstrated practically in the [definition for
big.js](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/big.js/index.d.ts), which is a library that can be loaded globally via script tag on a web page, or imported via require or ES6-style imports.
To test how your definition can be used both when referenced globally or as an imported module, create a
test folder, and place two test files in there. Name one
YourLibraryName-global.test.ts and the other
YourLibraryName-module.test.ts. The
global test file should exercise the definition according to how it would be used in a script loaded on a web page where the library is available on the global scope - in this scenario you should not specify an import statement. The
module test file should exercise the definition according to how it would be used when imported (including the
import statement(s)). If you specify a
files property in your
tsconfig.json file, be sure to include both test files. A
practical example of this is also available on the
big.js definition.
Please note that it is not required to fully exercise the definition in each test file - it is sufficient to test only the globally-accessible elements on the global test file and fully exercise the definition in the module test file, or vice versa.
What about scoped packages?
Types for a scoped package @foo/bar should go in types/foo__bar. Note the double underscore.
When dts-gen is used to scaffold a scoped package, the paths property has to be manually adapted in the generated tsconfig.json to correctly reference the scoped package:
- ``` json
- {
- "paths": {
- "@foo/*": ["foo__*"]
- }
- }
- ```
License
This project is licensed under the MIT license.
Copyrights on the definition files are respective of each contributor listed at the beginning of each definition file.