Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

New Rule: discourage type assertions in favor of type guards #1228

@adidahiya

Description

@adidahiya

Name: no-type-assertion

  • lines up with existing no-angle-bracket-type-assertion rule

Failing code:

function foo(myArg: any) {
  if (myArg.bar != null) {
    doTheThing(myArg as IBar);
  } else if (myArg.baz != null) {
    anotherThing(myArg as IBaz);
  } else {
    ...
  }
}

Passing code:

function foo(myArg: any) {
  if (isBar(myArg)) {
    doTheThing(myArg);
  } else if (isBaz(myArg)) {
    anotherThing(myArg);
  } else {
    ...
  }
}

function isBar(x: any): x is IBar {
  ...
}

function isBaz(x: any): x is IBaz {
  ...
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions