@definitelytyped/dtslint
TypeScript icon, indicating that this package has built-in type declarations

0.2.27 • Public • Published

dtslint tests a TypeScript declaration file for style and correctness. It will install typescript and eslint for you, so this is the only tool you need to test a type definition.

Lint rules new to dtslint are documented in the docs directory.

Just looking for a type testing tool?

If you are just looking for a TypeScript type testing tool, use:

Setup

If you are working on DefinitelyTyped, read the DefinitelyTyped README.

If you are writing the library in TypeScript, don't use dtslint. Use --declaration to have type definitions generated for you.

If you are a library author, read below.

Add types for a library (not on DefinitelyTyped)

dts-gen may help, but is not required.

Create a types directory. (Name is arbitrary.) Add "types": "types" to your package.json. Read more on bundling types here.

types/index.d.ts

Only index.d.ts and package.json need to be published to NPM. The other files will be required by Definitely Typed for testing. Write your type definitions in index.d.ts. Refer to the handbook or dts-gen's templates for how to do this.

types/tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "lib": [
            "es6",
        ],
        "noImplicitAny": true,
        "noImplicitThis": true,
        "strictNullChecks": true,
        "strictFunctionTypes": true,
        "types": [],
        "noEmit": true,
        "forceConsistentCasingInFileNames": true
    },
    "files": [
        "index.d.ts",
        "PACKAGE-NAME-tests.ts",
    ]
}

You may extend "lib" to, for example, ["es6", "dom"] if you need those typings. You may also have to add "target": "es6" if using certain language features.

types/package.json

{
    "private": true,
    "name": "@types/PACKAGE-NAME",
    "version": "1.2.9999",
    "projects": [
        "https://example.com/"
    ],
    "dependencies": {
        "@types/DEPENDENCY-1": "*",
        "@types/DEPENDENCY-2": "*"
    },
    "devDependencies": {
        "@types/PACKAGE-NAME": "workspace:."
    },
    "owners": [
        {
            "name": "My Self",
            "githubUsername": "ghost"
        }
    ]
}
  1. If the types are for a scoped package, you must name-mangle @scope/package to @types/scope__package.
  2. The major and minor versions should match some published version of the npm package. The patch version must be 9999; Definitely Typed will increment published patch versions starting at 0, and the patch version of the types will not match the patch version of the npm package.
  3. "projects" is a link to the source project.
  4. There might not be any dependencies if the types don't rely on anything but standard types.
  5. "devDependencies" must include a self-reference like "@types/PACKAGE-NAME": "workspace:.. Plus any dependencies used only by tests.
  6. Non-@types dependencies must be added to allowedPackageJsonDependencies.txt in the definitions-parser package. The PR must be approved by a Typescript team member.
  7. If you do not have a github user name, you can provide a "url" of your own instead.

Also:

For types that do not have a matching NPM package, add two properties:

  1. "nonNpm": true
  2. "nonNpmDescription", a human-readable name for the project that is being typed.

types/.eslintrc.json

An .eslintrc.json file is optional. You can skip it if you don't need to modify any lint rule settings. We recommend not adding an .eslintrc.json file.

{ "extends": "@definitelytyped/dtslint/dt.json" }

If present, this will override dtslint's default "all" config. You can specify new lint rules, or disable some. An example:

{
    "rules": {
        "@definitelytyped/no-unnecessary-generics": "off"
    }
}

Please don't do this without a good reason. Disabling lint rules makes a Definitely Typed PR less likely to be merged, and will definitely take longer to review.

types/test.ts

You can have any number of test files you want, with any names. See below on what to put in them.

Write tests

A test file should be a piece of sample code that tests using the library. Tests are type-checked, but not run. To assert that an expression is of a given type, use $ExpectType. To assert that an expression causes a compile error, use @ts-expect-error. (Assertions will be checked by the expect lint rule.)

import { f } from "my-lib"; // f is(n: number) => void

// $ExpectType void
f(1);

// Can also write the assertion on the same line (but not if it's a multiline function call).
f(2); // $ExpectType void

// @ts-expect-error
f("one");

Specify a TypeScript version

Normally packages will be tested according to DefinitelyType's support window. To restrict testing to new versions only, specify it in package.json:

"minimumTypeScriptVersion: 5.0"

This tests only 5.0 and above, although people can still depend on the package with lower versions of Typescript if they want.

Run tests

  • npm install --save-dev @definitelytyped/dtslint
  • Add to your package.json scripts: "dtslint": "dtslint types"
  • npm run dtslint

Options

  • --localTs

Use your locally installed version of TypeScript.

dtslint --localTs node_modules/typescript/lib types
  • --expectOnly

Disable all the lint rules except the one that checks for type correctness.

dtslint --expectOnly types

Contributing

Build

npm link . # Global 'dts-lint' should now refer to this.
npm run watch

Test

Use pnpm test to run all tests.

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

FAQ

I'm getting an error about a missing typescript install.

Error: Cannot find module '/node_modules/dtslint/typescript-installs/3.1/node_modules/typescript`

Your dependencies may be out of date. @definitelytyped/typescript-versions is the package that contains the list of TypeScript versions to install.

Alternatively this error can be caused by concurrent dtslint invocations trampling each other's TypeScript installations, especially in the context of continuous integration, if dtslint is installed from scratch in each run. If for example you use Lerna, try running dtslint with lerna --concurrency 1 run ....

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.0.175-next.10next
0.2.272,808latest

Version History

VersionDownloads (Last 7 Days)Published
0.2.272,808
0.2.261
0.2.251
0.2.2410
0.2.2341
0.2.2255
0.2.2121
0.2.2034
0.2.199
0.2.180
0.2.170
0.2.160
0.2.156
0.2.140
0.2.130
0.2.120
0.2.110
0.2.104
0.2.90
0.2.80
0.2.70
0.2.60
0.2.50
0.2.40
0.2.30
0.2.20
0.2.10
0.2.00
0.1.50
0.1.40
0.1.30
0.1.20
0.1.10
0.1.00
0.0.2040
0.0.2030
0.0.2020
0.0.2010
0.0.2000
0.0.19928,340
0.0.1980
0.0.1970
0.0.1960
0.0.1950
0.0.1940
0.0.1930
0.0.1920
0.0.1910
0.0.1907
0.0.1890
0.0.1880
0.0.1870
0.0.1860
0.0.1850
0.0.1840
0.0.1830
0.0.18273
0.0.1810
0.0.1800
0.0.1790
0.0.178410
0.0.1770
0.0.176527
0.0.1750
0.0.175-next.10
0.0.175-next.00
0.0.1740
0.0.174-next.00
0.0.1730
0.0.173-next.00
0.0.1720
0.0.172-next.00
0.0.1710
0.0.171-next.00
0.0.1700
0.0.170-next.10
0.0.170-next.00
0.0.1690
0.0.1680
0.0.168-next.10
0.0.168-next.00
0.0.1670
0.0.167-next.10
0.0.1660
0.0.166-next.40
0.0.166-next.30
0.0.166-next.10
0.0.166-next.00
0.0.1650
0.0.164-next.100
0.0.164-next.60
0.0.164-next.20
0.0.163162
0.0.163-next.10
0.0.1620
0.0.162-next.00
0.0.1610
0.0.161-next.00
0.0.1600
0.0.160-next.110
0.0.159122
0.0.159-next.10
0.0.159-next.00
0.0.1580
0.0.158-next.40
0.0.158-next.30
0.0.158-next.20
0.0.158-next.10
0.0.1570
0.0.157-next.00
0.0.1560
0.0.156-next.00
0.0.1550
0.0.154-next.20
0.0.154-next.00
0.0.1530
0.0.153-next.10
0.0.1521
0.0.152-next.80
0.0.152-next.50
0.0.152-next.30
0.0.1510
0.0.151-next.30
0.0.1500
0.0.150-next.10
0.0.150-next.00
0.0.1490
0.0.149-next.00
0.0.1480
0.0.148-next.80
0.0.148-next.70
0.0.148-next.60
0.0.148-next.50
0.0.148-next.40
0.0.148-next.30
0.0.148-next.10
0.0.1470
0.0.1460
0.0.1450
0.0.1440
0.0.1430
0.0.142272
0.0.142-next.120
0.0.142-next.100
0.0.142-next.90
0.0.1418
0.0.141-next.20
0.0.141-next.10
0.0.14014
0.0.140-next.00
0.0.1390
0.0.139-next.00
0.0.1380
0.0.1370
0.0.1360
0.0.135-next.40
0.0.135-next.30
0.0.1340
0.0.134-next.80
0.0.13338
0.0.1320
0.0.132-next.00
0.0.131-next.00
0.0.1300
0.0.130-next.00
0.0.1290
0.0.129-next.00
0.0.1280
0.0.128-next.30
0.0.12718,607
0.0.127-next.20
0.0.1260
0.0.126-next.10
0.0.1250
0.0.125-next.00
0.0.1240
0.0.124-next.00
0.0.1230
0.0.1220
0.0.122-next.110
0.0.122-next.634
0.0.122-next.30
0.0.122-next.00
0.0.12132
0.0.120-next.30
0.0.120-next.20
0.0.120-next.10
0.0.120-next.00
0.0.1190
0.0.119-next.50
0.0.119-next.30
0.0.1180
0.0.118-next.00
0.0.1170
0.0.1160
0.0.116-next.10
0.0.1158
0.0.115-next.10
0.0.115-next.01
0.0.1140
0.0.1130
0.0.113-next.220
0.0.113-next.170
0.0.113-next.70
0.0.113-next.40
0.0.113-next.31
0.0.113-next.20
0.0.113-next.00
0.0.112104
0.0.112-next.110
0.0.112-next.100
0.0.112-next.91
0.0.112-next.40
0.0.1110
0.0.111-next.20
0.0.1100
0.0.110-next.10
0.0.110-next.00
0.0.1090
0.0.109-next.00
0.0.1080
0.0.108-next.00
0.0.1070
0.0.107-next.00
0.0.1060
0.0.1050
0.0.1040
0.0.104-next.10
0.0.104-next.00
0.0.1030
0.0.1020
0.0.1010
0.0.101-next.30
0.0.1005
0.0.100-next.10
0.0.100-next.00
0.0.990
0.0.99-next.00
0.0.980
0.0.98-next.11
0.0.98-next.00
0.0.970
0.0.97-next.00
0.0.960
0.0.96-next.30
0.0.950
0.0.95-next.10

Package Sidebar

Install

npm i @definitelytyped/dtslint

Weekly Downloads

43,568

Version

0.2.27

License

MIT

Unpacked Size

102 kB

Total Files

24

Last publish

Collaborators

  • jakebailey
  • andrewbranch
  • types
  • sanders_n
  • rbuckton
  • sheetalkamat
  • weswigham
  • definitelytyped-publisher