Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

eslint-plugin-expect-type

JoshuaKGoldberg41.3kApache-2.00.6.2TypeScript support: included

ESLint plugin with ^? Twoslash, $ExpectError, and $ExpectType type assertions. 🧩

dtslint, d.ts, eslint, tslint, plugin, config, rule, typescript, ts, Expect, Assert, Type, Snapshot, $ExpectError, $ExpectType, $ExpectTypeSnapshot, twoslash, ^?

readme

eslint-plugin-expect-type

ESLint plugin with `^?` Twoslash, `$ExpectError`, and `$ExpectType` type assertions. 🧩

All Contributors: 13 👪 Contributor Covenant Codecov Test Coverage License: Apache-2.0 npm package version TypeScript: Strict

let value = 9001;
//  ^? let value: number

// $ExpectError
value = "over nine thousand";

// $ExpectType number
9001;

Installation

Make sure you have TypeScript and @typescript-eslint/parser installed, then install the plugin:

npm i -D eslint-plugin-expect-type

See typescript-eslint's Getting Started docs for how to run ESLint on TypeScript files.

Usage

Add the following options to your ESLint configuration file:

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.

Usage (Legacy Config)

If you're still using the legacy ESLint configuration file format:

{
    "extends": ["plugin:expect-type/recommended"],
    "plugins": ["expect-type"]
}

Rules

💼 Configurations enabled in.\ ✅ Set in the recommended configuration.\ 🔧 Automatically fixable by the --fix CLI option.\ 💭 Requires type information.

Name Description 💼 🔧 💭
expect Expects type error, type snapshot, or type. 🔧 💭

References

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<string>().
  • ts-expect: Provides generic type assertion function, used like expectType<string>('abc')().
  • tsd: Allows writing tests specifically for .d.ts definition files.
  • TSTyche: A type testing tool that ships with describe() and test() helpers, expect style assertions and a mighty test runner which allows to use specified version of TypeScript.
  • Vitest: Includes assertType and expectTypeOf assertions.

TypeScript Version Support

eslint-plugin-expect-type mirrors the DefinitelyTyped TypeScript Support Window. Roughly, that's major versions of TypeScript less than 2 years old.

Appreciation

Many thanks to @ibezkrovnyi for creating the initial version and core infrastructure of this package! 💖

Contributors

Batuhan Wilhelm
Batuhan Wilhelm

🐛 💻
Colin
Colin

🐛
Cédric Exbrayat
Cédric Exbrayat

🐛
Dan Vanderkam
Dan Vanderkam

💻 🚧
Daniel Nagy
Daniel Nagy

🐛
Dominik Dorfmeister
Dominik Dorfmeister

📖
Francesco Trotta
Francesco Trotta

💻 🐛
Igor Bezkrovnyi
Igor Bezkrovnyi

🐛 💻 📖 🚧
Josh Goldberg ✨
Josh Goldberg ✨

🐛 💻 📖 🚧 🚇 🤔 🔧
Russell Davis
Russell Davis

💻
Tom Mrazauskas
Tom Mrazauskas

📖
detachhead
detachhead

🤔
nirtamir2
nirtamir2

📖

💙 This package was templated with create-typescript-app.

changelog

0.6.1 (2024-11-29)

Bug Fixes

  • clear programs cache if >90% of heap is used (#643) (9596a34), closes #642

0.6.0 (2024-11-29)

Features

  • add versionsToTest option to support multiple TypeScript versions (#635) (153f83e), closes #107

0.5.1 (2024-11-29)

Bug Fixes

0.5.0 (2024-11-28)

Features

0.4.3 (2024-09-12)

Bug Fixes

0.4.2 (2024-09-10)

Bug Fixes

0.4.1 (2024-09-09)

Bug Fixes

0.4.0 (2024-03-26)

Features

0.3.0 (2023-12-20)

Bug Fixes

  • also replace \r in string whitespace (#73) (19ac77e)
  • factor all whitespace in first-on-line check (#86) (a3d5499)
  • only update type snapshots if process.argv includes --fix (#113) (d384c6d), closes #14

Features