Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eslint-plugin-expect-type

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-expect-type

ESLint plugin with $ExpectType, $ExpectError and $ExpectTypeSnapshot type assertions

  • 0.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7.5K
increased by28.88%
Maintainers
1
Weekly downloads
 
Created
Source

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

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

Rule severity could be configured as follows

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

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 Foo
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

Keywords

FAQs

Package last updated on 08 Sep 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc