JSPM

eslint-plugin-no-type-assertion

1.3.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 83223
  • Score
    100M100P100Q180374F
  • License MIT

Disallow type assertions in TypeScript code

Package Exports

  • eslint-plugin-no-type-assertion
  • eslint-plugin-no-type-assertion/lib/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (eslint-plugin-no-type-assertion) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

eslint-plugin-no-type-assertion

Disallow type assertions in TypeScript code. The rule will forbid both as operator and the angle-bracketed syntax, unless used for const assertions or with the unknown type. The rule also forbids non-null assertions.

The following code becomes invalid:

const foo: unknown = 42;

const notAString = <string>foo; // can't perform angle-bracketed type assertion
const alsoNotAString = foo as string; // can't use `as` operator for type assertion

const objectWithOptionalProperty: { a?: number } = {};

const notANumber = objectWithOptionalProperty.a! / 2; // can't use non-null assertion operator

Installation

Ensure you have ESLint and @typescript-eslint/parser installed:

$ yarn add eslint @typescript-eslint/parser -D

Next, install this plugin:

$ yarn add eslint-plugin-no-type-assertion -D

Configuration

In your ESLint configuration, ensure you're using @typescript-eslint/parser as a parser and it's set up correctly:

{
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaVersion": 2018
  }
}

Add eslint-plugin-no-type-assertion to the plugin list:

{
  "plugins": ["no-type-assertion"]
}

Enable the rule:

{
  "rules": {
    "no-type-assertion/no-type-assertion": "error"
  }
}