Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Rule proposal: no-unsafe-type-assertion #7173

Closed
Labels
accepting prsGo ahead, send a pull request that resolves this issueenhancement: new plugin ruleNew rule request for eslint-pluginlocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin
@lgenzelis

Description

@lgenzelis

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
  • My proposal is not a "formatting rule"; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Description

One of the hardest thing to explain to people not experienced in TS is when it's ok to useas. I default to tell people "don't useas", and I constantly have to deal with that in code reviews. I'd love to have a rule that only allows type assertions that widen a type. For example,as unknown is always totally ok.as string | number is safe on astring, but unsafe in astring | boolean.

This rule should only allow doingx as Type in the cases where the type ofx is assignable totype. So, the rule of thumb would be: ifconst a: Type = x is fine, thenx as Type is fine. Otherwise it's not.

Fail Cases

constx='something';consty=xasunknownasnumber;// Type `unknown` is not assignable to type `number` 🚫//-----------functionf(){returnMath.random()<0.5 ?42 :'meh';}constz=f()asnumber;// Type `number | string` is not assignable to type `number` 🚫

Pass Cases

consta='something';constb=aasunknown;// Type `string` is assignable to type `unknown` ✅//-----------functiong(){returnMath.random()<0.5 ?42 :'meh';}consth=f()asnumber|string|boolean;// Type `number | string` is assignable to type `number | string | boolean` ✅//-----------constperson={name:'Chuck',lastName:'Norris'asstring|undefined// Type `Norris` is assignable to type `string | undefined`  ✅}person.lastName=undefined;// this is fine because of the type assertion (just showing why we'd want the `as string | undefined`)

Additional Info

Here's a link to a TS playground with the passing and (proposed) failing cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancement: new plugin ruleNew rule request for eslint-pluginlocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp