ESLint plugin with ^? Twoslash, $ExpectError, and $ExpectType type assertions. π§©
npm install eslint-plugin-expect-typeESLint plugin with ^? Twoslash, $ExpectError, and $ExpectType type assertions. π§©
``ts
let value = 9001;
// ^? let value: number
// $ExpectError
value = "over nine thousand";
// $ExpectType number
9001;
`
Make sure you have TypeScript and @typescript-eslint/parser installed, then install the plugin:
`sh`
npm i -D eslint-plugin-expect-type
> See typescript-eslint's Getting Started docs for how to run ESLint on TypeScript files.
Add the following options to your ESLint configuration file:
`ts
import expectType from "eslint-plugin-expect-type/configs/recommended";
export default [
// your other ESLint configurations
expectType,
];
`
> For CommonJS, use const expectType = require("eslint-plugin-expect-type/configs/recommended").default;.
Then, you'll be able to use ^?, $ExpectError, $ExpectType, and $ExpectTypeSnapshot comments in code assert on types.
If you're still using the legacy ESLint configuration file format:
`json`
{
"extends": ["plugin:expect-type/recommended"],
"plugins": ["expect-type"]
}
πΌ Configurations enabled in.\
β
Set in the recommended configuration.\--fix
π§ Automatically fixable by the CLI option.\
π Requires type information.
| Name | Description | πΌ | π§ | π |
| :----------------------------- | :------------------------------------------ | :- | :- | :- |
| expect | Expects type error, type snapshot, or type. | β
| π§ | π |
You might consider using other popular libraries and tools that can run type assertions:
- expect-type: Provides functions that return assorted generic type assertion methods, such as expectTypeOf('abc').toMatchTypeOf.expectType
- ts-expect: Provides generic type assertion function, used like ..d.ts
- tsd: Allows writing tests specifically for definition files.describe()
- TSTyche: A type testing tool that ships with and test() helpers, expect style assertions and a mighty test runner which allows to use specified version of TypeScript.assertType
- Vitest: Includes and expectTypeOf assertions.
eslint-plugin-expect-type` mirrors the DefinitelyTyped TypeScript Support Window.
Roughly, that's major versions of TypeScript less than 2 years old.
Many thanks to @ibezkrovnyi for creating the initial version and core infrastructure of this package! π
> π This package was templated with create-typescript-app.