TypeScript linter

TypeScript is being used as a type checker for JavaScript.

This is a new linter that is being set up and readied for production use. Please see this page before setting up new projects.

Run Locally

The mozlint integration of TypeScript can be run using mach:

$ mach lint --linter typescript <file paths>

Alternatively, omit the --linter typescript and run all configured linters, which will include TypeScript.

TypeScript does not support the --fix option.

See the Usage guide for more options.

Understanding Rules and Errors

  • Only some files are linted, see the configuration for details.

    • By design we do not lint/format reftests nor crashtests as these are specially crafted tests.

Common Issues and How To Solve Them

The TypeScript type information needs updating due to source code changes

See Updating Gecko Type Definitions

This code should neither be linted nor formatted

Configuration

Individual components are configured via TypeScript tsconfig.json files in the project directory. See Enabling TypeScript for a Component for how to set one up.

Which Projects a Run Checks

The linter checks only the projects listed in tools/lint/typescript.yml. For each path you give it, the linter finds the listed project that contains the path. It then checks that whole project, not just the path. If the path is a parent directory, such as browser/components, the linter checks every listed project inside it.

If you run mach lint from the top of the tree without a path, it checks only the listed projects that contain files changed in your outgoing commits and working directory. If you have no local changes, it checks nothing and reports no problems. If one of the changed files is under tools/@types, it checks every listed project. To check every listed project yourself, pass .:

./mach lint -l typescript .

Note

A passing run doesn’t mean the linter checked the file you changed. TypeScript reports errors only in files that match the project’s include list and that its exclude list doesn’t remove. Look at both lists before you rely on the result.

If a path is not in a listed project, the linter checks nothing and reports success. This also happens for a directory that has a tsconfig.json but isn’t listed in typescript.yml. To check an unlisted project, run:

./mach ts check path/to/dir

An unlisted project usually still has errors, which makes it hard to see which errors a change added or removed. You can save a run before the change, and then compare the run after it with the saved one:

./mach ts summary path/to/dir --save before.txt
./mach ts summary path/to/dir --diff before.txt

Files Outside the Project Directory

A project’s tsconfig.json can include a file from another directory, such as another component’s types/*.ts file. The linter checks a project only when a changed path is inside the project’s directory. So when you change that file, the linter doesn’t check the projects that include it. Files under tools/@types are the exception: changing one makes a run without a path check every listed project.

The same applies when you change the types that a module exports. A caller in another project can break, and the linter won’t check that project. After either kind of change, check every listed project with ./mach lint -l typescript ..

That run checks the code that uses a declaration file under tools/@types, but not the file itself unless a project lists it in include. A project reports errors only in the files that its include list matches, and projects load most of these files through tools/@types/index.d.ts without listing them, e.g. lib.gecko.augmentations.d.ts and the generated lib.gecko.*.d.ts files. So an unknown type name in such a file reads as any, and a conflicting second declaration of a name can go unreported. To check the declaration files themselves, run:

./mach ts check tools/@types/check

That project isn’t listed in typescript.yml, and it reports errors that the files already have, so compare its output before and after your change.

In CI

The typescript task, shown as js(TSC) on Treeherder, runs ./mach lint -l typescript ., so it checks every listed project. The task runs only when a changed file matches one of the paths that trigger it. These paths are the files-changed list in taskcluster/kinds/source-test/mozlint.yml. The list covers the JavaScript and TypeScript files in each listed directory, every tsconfig.json, the files under tools/@types, and the linter’s own files. If a patch changes only files outside that list, the task doesn’t run, even if a project includes one of those files.

Sources

Builders

Mark Banner (standard8) owns the builders. Questions can also be asked on #lint:mozilla.org on Matrix.

TypeScript task

This task is tier 2. It also reports via the code review bot. Issues should be fixed if possible. If uncertain check with the appropriate team.

For test harness issues, file bugs in Developer Infrastructure :: Lint and Formatting.