Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
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
Description
Before You File a Proposal Please Confirm You Have Done The Following...
- I havesearched for related issues and found none that match my proposal.
- I have searched thecurrent rule list and found no rules that match my proposal.
- I haveread the FAQ and my problem is not listed.
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
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