Are the types wrong?
Special thanks to @userquin for his help and his PR inspired me to write this article.
Concise and to the point. If there are any inaccuracies, please correct me. Also, refer to the official documentation arethetypeswrong
What is arethetypeswrong?
arethetypeswrong (abbreviated as attw below) is a tool for analyzing TypeScript types of npm packages. By analyzing the type definition files of npm packages, attw finds problems in them and provides suggestions for fixing them. The goal of attw is to help developers better understand the type definitions of npm packages and improve code quality.
It can help you analyze and detect the following problems:
- π Resolution failed
- β No types
- π Masquerading as CJS
- πΊ Masquerading as ESM
- β οΈ ESM (dynamic import only)
- π Used fallback condition
- π€¨ CJS default export
- βοΈ Incorrect default export
- β Missing
export = - π Unexpected module syntax
- π₯΄ Internal resolution error
- π΅οΈββοΈ Named exports
How to use arethetypeswrong?
You can use it online at https://arethetypeswrong.github.io/ or use the CLI tool in your project.
pnpm i -D @arethetypeswrong/cli
δ½Ώη¨ attw --pack ζ£ζ₯εδΈͺε
οΌε¨ζ¨ηscriptsδΈζ·»ε οΌ
Use attw --pack to check a single package, add it to your scripts:
{
"scripts": {
"attw": "attw --pack"
}
}
Or without installing, use npx directly:
npx --yes @arethetypeswrong/cli
Check packages from npm:
npx --yes @arethetypeswrong/cli --from-npm @arethetypeswrong/cli
Configuration
attw supports a JSON config file (by default named .attw.json) which allows you to pre-set the command line arguments. The options are a one-to-one mapping of the command line flags, changed to camelCase, and are all documented in their relevant Options section below.
Options
Help
Show help information and exit.
In the CLI: --help, -h
attw --help
Version
Print the current version of attw and exit.
In the CLI: --version, -v
attw --version
Pack
Specify a directory to run npm pack in (instead of specifying a tarball filename), analyze the resulting tarball, and delete it afterwards.
attw --pack .
From NPM
Specify the name (and, optionally, version or SemVer range) of a package from the NPM registry instead of a local tarball filename.
In the CLI: --from-npm, -p
attw --from-npm <package-name>
In the config file, fromNpm can be a boolean value.
DefinitelyTyped
When a package does not contain types, specifies the version or SemVer range of the DefinitelyTyped @types package to use. Defaults to inferring the best version match from the implementation package version.
In the CLI: --definitely-typed, --no-definitely-typed
attw -p <package-name> --definitely-typed <version>
attw -p <package-name> --no-definitely-typed
Format
The format to print the output in. Defaults to auto.
The available values are:
table, where columns are entrypoints and rows are resolution kindstable-flipped, where columns are resolution kinds and rows are entrypointsascii, for large tables where the output is clunkyauto, which picks whichever of the above best fits the terminal widthjsonoutputs the raw JSON data (overriding all other rendering options)
In the CLI: --format, -f
attw --format <format> <file-name>
In the config file, format can be a string value.
Entrypoints
attw automatically discovers package entrypoints by looking at package.json exports and subdirectories with additional package.json files. In a package lacking exports, providing the --entrypoints-legacy option will include all published code files. This automatic discovery process can be overridden with the --entrypoints option, or altered with the --include-entrypoints and --exclude-entrypoints options:
attw --pack . --entrypoints . one two three # Just ".", "./one", "./two", "./three"
attw --pack . --include-entrypoints added # Auto-discovered entrypoints plus "./added"
attw --pack . --exclude-entrypoints styles.css # Auto-discovered entrypoints except "./styles.css"
attw --pack . --entrypoints-legacy # All published code files
Profiles
Profiles select a set of resolution modes to require/ignore. All are evaluated but failures outside of those required are ignored.
The available profiles are:
strict- requires all resolutionsnode16- ignores node10 resolution failuresesm-only- ignores CJS resolution failures
In the CLI: --profile
attw <file-name> --profile <profile>
In the config file, profile can be a string value.
Ignore Rules
Specifies rules/problems to ignore (i.e. not raise an error for).
The available values are:
no-resolutionuntyped-resolutionfalse-cjsfalse-esmcjs-resolves-to-esmfallback-conditioncjs-only-exports-defaultfalse-export-defaultunexpected-module-syntaxmissing-export-equalsinternal-resolution-errornamed-exports
In the CLI: --ignore-rules
attw <file-name> --ignore-rules <rules...>
In the config file, ignoreRules can be an array of strings. For example, to ignore no-resolution and untyped-resolution in config file:
{
"ignoreRules": ["no-resolution", "untyped-resolution"]
}
Summary/No Summary
Whether to display a summary of what the different errors/problems mean. Defaults to showing the summary (--summary).
In the CLI: --summary/--no-summary
attw --summary/--no-summary <file-name>
In the config file, summary can be a boolean value.
Emoji/No Emoji
Whether to print the information with emojis. Defaults to printing with emojis (--emoji).
In the CLI: --emoji/--no-emoji
attw --emoji/--no-emoji <file-name>
In the config file, emoji can be a boolean value.
Color/No Color
Whether to print with colors. Defaults to printing with colors (--color).
The FORCE_COLOR env variable is also available for use (set is to 0 or 1).
In the CLI: --color/--no-color
attw --color/--no-color <file-name>
In the config file, color can be a boolean value.
Quiet
When set, nothing will be printed to STDOUT.
In the CLI: --quiet, -q
attw --quiet <file-name>
In the config file, quiet can be a boolean value.
Config Path
The path to the config file. Defaults to ./.attw.json.
In the CLI: --config-path <path>
attw --config-path <path> <file-name>
Cannot be set from within the config file itself.