All Projects → JoshuaKGoldberg → eslint-plugin-expect-type

JoshuaKGoldberg / eslint-plugin-expect-type

Licence: Apache-2.0 License
ESLint plugin with $ExpectType, $ExpectError, and $ExpectTypeSnapshot type assertions

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to eslint-plugin-expect-type

eslint-plugin-lodash-template
ESLint plugin for John Resig-style micro template, Lodash's template, Underscore's template and EJS.
Stars: ✭ 15 (-44.44%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-disable
Disable ESLint plugins using file path patterns and inline comments
Stars: ✭ 51 (+88.89%)
Mutual labels:  eslint, eslint-plugin
html-eslint
ESLint plugin for linting HTML
Stars: ✭ 72 (+166.67%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin Mocha
ESLint rules for mocha
Stars: ✭ 249 (+822.22%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-layout-shift
ESLint plugin to force responsive media elements to set the width/height attributes
Stars: ✭ 15 (-44.44%)
Mutual labels:  eslint, eslint-plugin
typescript-eslint-demo
typescript eslint playground
Stars: ✭ 25 (-7.41%)
Mutual labels:  eslint-plugin, typescript-eslint
eslint-plugin-editorconfig
An ESLint plugin to enforce EditorConfig rules
Stars: ✭ 22 (-18.52%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin Lodash
ESLint rules for lodash
Stars: ✭ 208 (+670.37%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-ban
Ban some methods and functions
Stars: ✭ 23 (-14.81%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-react-hook-form
ESLint plugin for react-hook-form
Stars: ✭ 27 (+0%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin Ember
An ESlint plugin that provides set of rules for Ember Applications based on commonly known good practices.
Stars: ✭ 240 (+788.89%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin
autofix some errors reported by eslint rules.
Stars: ✭ 74 (+174.07%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin Eslint Comments
Additional ESLint rules for directive comments of ESLint.
Stars: ✭ 221 (+718.52%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-pug
An ESLint plugin for linting inline scripts in Pug files
Stars: ✭ 17 (-37.04%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin Ava
ESLint rules for AVA
Stars: ✭ 209 (+674.07%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-rulesdir
An ESLint plugin to load project-specific ESLint rules
Stars: ✭ 28 (+3.7%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin Unicorn
Various awesome ESLint rules
Stars: ✭ 2,157 (+7888.89%)
Mutual labels:  eslint, eslint-plugin
Eslint Plugin Boundaries
Eslint plugin checking architecture boundaries between elements
Stars: ✭ 157 (+481.48%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-svelte
ESLint plugin for Svelte using AST
Stars: ✭ 22 (-18.52%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-total-functions
An ESLint plugin to enforce the use of total functions (and prevent the use of partial functions) in TypeScript.
Stars: ✭ 72 (+166.67%)
Mutual labels:  eslint, eslint-plugin

eslint-plugin-expect-type

ESLint plugin with $ExpectType and $ExpectError type assertions

Installation

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

npm i -D eslint-plugin-expect-type

Please also make sure the following packages are also installed:

npm i -D eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser

Usage

Please add the following options to your .eslintrc

{
  "extends": ["plugin:eslint-plugin-expect-type/recommended"],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "./tsconfig.json"
  },
  "plugins": ["eslint-plugin-expect-type"],
  "root": true
}

Rule severity could be configured as follows

{
  "rules": {
    "expect-type/expect": "error"
  }
}

To skip Snapshot update when eslint is run with --fix could be configured as follows:

{
  "rules": {
    "expect-type/expect": ["error", { "disableExpectTypeSnapshotFix": true }]
  }
}

Note: Make sure to use eslint --ext .js,.ts since by default eslint will only search for .js files.

Adding $ExpectType and $ExpectError type assertions

A test file should be a piece of sample code that tests using the library. Tests are type-checked, but not run. To assert that an expression is of a given type, use $ExpectType. To assert that an expression causes a compile error, use $ExpectError. (Assertions will be checked by the expect lint rule.)

(https://github.com/Microsoft/dtslint#write-tests)

import foo from 'lib-to-test'; // foo is (n: number) => void

// $ExpectType void
foo(1);

// Can also write the assertion on the same line.
foo(2); // $ExpectType void

// $ExpectError
foo('bar');

Adding $ExpectTypeSnapshot

Uses snapshot saved in file as expected type for expression.

Example:

foo.test.ts

// $ExpectTypeSnapshot MyFooSnapshot
const Foo = {
  a: 1,
  n: 17,
} as const;

By running eslint --fix the following file will be created in the folder of foo.test.ts:

__type-snapshots__/foo.test.ts.snap.json

By running eslint snapshot type will be matched with actual type and Error will be emitted in case types don't match.

To create/update snapshots

eslint --fix

References

  1. https://github.com/gcanti/dtslint
  2. https://github.com/Microsoft/dtslint
  3. https://github.com/SamVerschueren/tsd

Appreciation

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].