eslint-plugin-expect-type
raw JSON → 0.6.2 verified Sat Apr 25 auth: no javascript
ESLint plugin that adds Twoslash-style type assertions ($ExpectType, $ExpectError, $ExpectTypeSnapshot) and the ^? hover type comment. Version 0.6.2 is the latest stable release as of early 2025, with active development and frequent releases (0.4.x to 0.6.x in rapid succession). Unlike runtime assertion libraries (expect-type, ts-expect), this plugin works at the ESLint linting level without test runners. Requires @typescript-eslint/parser ≥6, ESLint ≥7, and TypeScript ≥4. Unique for inline type assertions in comments, enabling type-level testing in any file linted by ESLint.
Common errors
error Error: Failed to load plugin 'expect-type' declared in 'plugins': Cannot find module 'eslint-plugin-expect-type' ↓
cause Plugin is not installed in the project's node_modules.
fix
npm install eslint-plugin-expect-type --save-dev
error TypeError: expectType is not a function ↓
cause Using require() on configs/recommended without .default in CommonJS.
fix
Use require('eslint-plugin-expect-type/configs/recommended').default
error ESLint: Unexpected $ExpectType comment (expect-type/expect) ↓
cause The $ExpectType comment is used but the expect rule is not enabled or the comment syntax is incorrect.
fix
Enable 'expect-type/expect' rule in ESLint config and ensure the comment is on its own line before the expression.
error Parsing error: Invalid or unexpected token ↓
cause TypeScript syntax used in a .js file without @typescript-eslint/parser configured.
fix
Set parser: '@typescript-eslint/parser' in ESLint config for TypeScript files.
Warnings
breaking v0.6.0 adds versionsToTest option but may require updating configuration if you used the old undocumented API. ↓
fix Migrate to explicit versionsToTest array in plugin options if needed.
gotcha The plugin relies on TypeScript's language service and can be memory-intensive for large projects; a heap cache limit was introduced in v0.6.1. ↓
fix Upgrade to >=0.6.1 to avoid potential out-of-memory crashes.
gotcha CommonJS users must use require('...').default when importing the configs/recommended path. ↓
fix Use const expectType = require('eslint-plugin-expect-type/configs/recommended').default;
deprecated The legacy ESLint config format (plugin:expect-type/recommended) is deprecated in favor of flat config. ↓
fix Switch to flat configuration using import expectType from 'eslint-plugin-expect-type/configs/recommended'.
gotcha $ExpectType and $ExpectError comments must be on the line before the expression or statement, not on the same line. ↓
fix Place the comment on its own line immediately above the line to be checked.
gotcha The ^? Twoslash hover type comment must be placed directly after the variable/expression, with exactly one space between the variable and //. ↓
fix Write: let value = 9001; // ^? let value: number
Install
npm install eslint-plugin-expect-type yarn add eslint-plugin-expect-type pnpm add eslint-plugin-expect-type Imports
- default wrong
const expectType = require('eslint-plugin-expect-type/configs/recommended')correctimport expectType from 'eslint-plugin-expect-type/configs/recommended' - rules wrong
const { rules } = require('eslint-plugin-expect-type')correctimport { rules } from 'eslint-plugin-expect-type' - plugin wrong
import { expectType } from 'eslint-plugin-expect-type'correctimport expectTypePlugin from 'eslint-plugin-expect-type'
Quickstart
// eslint.config.js
import expectType from 'eslint-plugin-expect-type/configs/recommended';
export default [
expectType,
{
files: ['**/*.ts'],
rules: {
'expect-type/expect': 'warn',
'expect-type/expect-error': 'error',
},
},
];
// my-file.ts
let value = 9001;
// ^? let value: number
// $ExpectError
value = "over nine thousand";
// $ExpectType number
9001;
// $ExpectTypeSnapshot ["number"]
type X = 1;