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

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

Closed
adidahiya opened this issue May 9, 2016 · 6 comments
Closed

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

adidahiya opened this issue May 9, 2016 · 6 comments

Comments

@adidahiya
Copy link
Contributor

adidahiya commented May 9, 2016

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 {
  ...
}
@jskrzypek

This comment has been minimized.

@adidahiya adidahiya added the P2 label May 27, 2016
@woutervh-

This comment has been minimized.

@JoshuaKGoldberg
Copy link
Contributor

JoshuaKGoldberg commented Jan 2, 2019

Relevant: discussion in #2040 on bringing in the equivalent rule from tslint-consistent-codestyle.

IMO it'd be better to name this type-assertion and allow configuring to:

  • Ban type guardsassertions (default; enabled in tslint:all)
  • Allow as Type (enabled in tslint:recommended, tslint:latest)
  • Allow <Type>

Note that as ajafff mentioned, copied code without substantial changes needs to retain the original license.

@JoshuaKGoldberg
Copy link
Contributor

Note: per #4534, this issue will be closed in less than a month if no PR is sent to add the rule. If you really need the rule, custom rules are always an option and can be maintained outside this repo!

@JoshuaKGoldberg
Copy link
Contributor

💀 It's time! 💀

TSLint is being deprecated and no longer accepting pull requests for new rules. See #4534. 😱

If you'd like to see this rule implemented, you have two choices:

👋 It was a pleasure open sourcing with you!

If you believe this message was posted here in error, please comment so we can re-open the issue!

@JoshuaKGoldberg
Copy link
Contributor

🤖 Beep boop! 👉 TSLint is deprecated 👈 (#4534) and you should switch to typescript-eslint! 🤖

🔒 This issue is being locked to prevent further unnecessary discussions. Thank you! 👋

@palantir palantir locked and limited conversation to collaborators Mar 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants