A utility built on TSLint for linting TypeScript declaration (.d.ts) files.
Перейти к файлу
Nathan Shively-Sanders 1ccb5736f1 Update README with new package location
dtslint -> @definitelytyped/dtslint
2021-12-02 11:33:48 -08:00
.github/workflows Rename CI 2020-07-23 18:22:49 -04:00
docs no-redundant-undefined: enforce `undefined` for optional parameters (#335) 2021-07-08 15:12:43 -07:00
src Allow package.json files to specify imports/exports/type 2021-10-13 14:51:24 -07:00
test Remove 'external module' wording from rules (#340) 2021-08-11 17:18:25 -07:00
.eslintrc.json use eslint on this repo (#318) 2021-03-25 16:39:45 -07:00
.gitignore Add tests 2017-04-06 08:39:44 -07:00
CODEOWNERS Add CODEOWNERS so PRs ping me 2019-06-24 09:40:36 -07:00
LICENSE Initial commit 2017-02-21 14:38:40 -08:00
README.md Update README with new package location 2021-12-02 11:33:48 -08:00
dt.json Update and re-enable tslint rules (#296) 2020-05-29 10:47:04 -07:00
dtslint-expect-only.json Add --expectOnly mode for TS@next regression testing 2019-02-20 11:33:43 -08:00
dtslint.json no-redundant-undefined: enforce `undefined` for optional parameters (#335) 2021-07-08 15:12:43 -07:00
package-lock.json 4.2.1 - update @definitelytyped and dts-critic dependencies 2021-11-18 11:28:56 -08:00
package.json 4.2.1 - update @definitelytyped and dts-critic dependencies 2021-11-18 11:28:56 -08:00
tsconfig.json Update to 4.0, deprecate 2.8 (#287) 2020-04-29 07:55:47 -07:00
tslint.json Fix lint 2019-02-06 09:22:29 -08:00

README.md

This repo has moved: dtslint is now part of DefinitelyTyped-tools.

It is not intended to be used on its own, but as part of the @definitelytyped set of packages. The source code has moved to https://github.com/microsoft/DefinitelyTyped-tools The new package name is @definitelytyped/dtslint.

dtslint tests a TypeScript declaration file for style and correctness. It will install typescript and tslint 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 ExpectType and ExpectError?

Use tsd instead.

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 needs to be published to NPM. Other files are just for testing. Write your type definitions here. 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,
        "strictFunctionTypes": true,
        "strictNullChecks": true,
        "types": [],
        "noEmit": true,
        "forceConsistentCasingInFileNames": true,

        // If the library is an external module (uses `export`), this allows your test file to import "mylib" instead of "./index".
        // If the library is global (cannot be imported via `import` or `require`), leave this out.
        "baseUrl": ".",
        "paths": { "mylib": ["."] }
    }
}

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

If you are using the default rules, this is optional.

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

{
    "extends": "dtslint/dtslint.json", // Or "dtslint/dt.json" if on DefinitelyTyped
    "rules": {
        "semicolon": false,
        "indent": [true, "tabs"]
    }
}

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 $ExpectError. (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.
f(2); // $ExpectType void

// $ExpectError
f("one");

Specify a TypeScript version

Normally packages will be tested using TypeScript 2.0. To use a newer version, specify it by including a comment like so:

// Minimum TypeScript Version: 2.1

For DefinitelyTyped packages, this should go just under the header (on line 5). For bundled typings, this can go on any line (but should be near the top).

Run tests

  • npm install --save-dev 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 npm run test to run all tests. To run a single test: node node_modules/tslint/bin/tslint --rules-dir bin/rules --test test/expect.

Publish

  1. Change the version in the package.json
  2. Push to master

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