Comments (4)
Funnily there used to be two separate rules.
- no-angle-bracket-type-assertion and
- no-object-literal-type-assertion
In v2 we merged them into one rule
https://github.com/typescript-eslint/typescript-eslint/releases/tag/v2.0.0 // #731
The reason we merged them was because people were asking for a way to completely ban assertion syntax.
Configuring multiple rules around assertions would have been tricky - so we merged them to reduce surface area and confusion.
From my experience in modern TS people don't ever even think of using angle-bracket assertions. So really by-and-large the rule only really enforces the object literal assertions.
All that being said - I don't think splitting the rules makes sense.
If someone wants to ban all assertions but also really strongly ban the angle bracket style them this FAQ explains how they can do that - https://typescript-eslint.io/troubleshooting#how-can-i-ban-specific-language-feature
from typescript-eslint.
The reason we merged them was because people were asking for a way to completely ban assertion syntax. Configuring multiple rules around assertions would have been tricky - so we merged them to reduce surface area and confusion.
Maybe so... But I, personally, feel very confused by the current setup. Normally when rules have multiple areas that they flag in, it's because they're different instances of the same kind of issue. So I spent a(n) (embarassingly) long time when I was first setting up this rule trying to figure out how object literal type assertions differ based on the syntax used, i.e. why <T>{}
might be different from {} as T
, in the same way that there is a real difference between, for example, the syntaxes in method-signature-style. But nope! complete red herring. They're just unrelated concerns.
From my experience in modern TS people don't ever even think of using angle-bracket assertions. So really by-and-large the rule only really enforces the object literal assertions.
So far every codebase at my company uses assertionStyle: 'angle-bracket'
. Not really sure why but I'm guessing some/all have existed since before as
, and/or the devs learned TS back then and never changed habits, so they set that up for consistency and no one has championed getting rid of that kinda-but-not-overtly-deprecated syntax (well, no one but me lol).
If someone wants to ban all assertions but also really strongly ban the angle bracket style them this FAQ explains how they can do that - https://typescript-eslint.io/troubleshooting#how-can-i-ban-specific-language-feature
Sure... but that's a team-member/super-user build-your-own lint rule approach that I could do today, but did not and would not have remotely considered as a user. Plus it obviously doesn't have an autofix, which is a huge appeal in using consistent-type-assertions
(i.e. as a codemod tool to migrate to as
).
The use case I had/still have is I want to configure different severities for the different parts of this rule. type assertion syntax? error! autofix em! done! object literal type assertions? this is a program logic error, so warn, and people can gradually address them over time if they happen to be looking at the code anyway, but it's nothing to get too stressed about going out of your way to fix.
When I first configured typescript-eslint for my team, I gave up on trying to achieve that, since, at the time, I did not have the knowledge of the existence of no-restricted-syntax or of how to use it. I would just hate for other people in the wild to have the same experience and give up 🤷♂️
from typescript-eslint.
So far every codebase at my company uses assertionStyle: 'angle-bracket'
That is definitely the outlier - most people stopped using angle brackets when as
was added because the syntax is much better.
Eg there's barely two dozen hits on sourcegraph for angle bracket vs 10x that for as.
Plus it obviously doesn't have an autofix, which is a huge appeal in using consistent-type-assertions (i.e. as a codemod tool to migrate to as).
The rule specifically has an autofixer just for the angle bracket to as case - so you can turn the rule on either as error or warning and you still get the migration power.
I.e. You turn the rule on as a warning (with the setting in as
) and no-restricted-syntax
on as an error - and you get the best of both worlds.
Though it's worth noting that migrations are not a usecase we consider a priority. Migrations are short-term - usually once-off but linting is forever.
from typescript-eslint.
The rule purposely hasn't been in the recommended set in a long time (the choice is a stylistic one even if the majority of people follow it).
I'd hazard a guess that many codebases don't even use the rule because it's not in recommended. I do know a few codebases that use .tsx
everywhere to make TS ban the angle bracket assertion outright (linters can be disabled but syntax errors cannot)
from typescript-eslint.
Related Issues (20)
- Bug: [no-unnecessary-type-assertion] false positive when casting to HTMLElement (from Element) HOT 6
- Bug: object-shorthand loses type parameters when auto-fixing HOT 3
- Enhancement: [strict-boolean-expressions] Check truthiness assertion functions HOT 6
- Repo: rule-tester's eslint-base test failing on main
- Repo: rule-tester tests failing on v8 HOT 1
- Bug: <short description of the issue> HOT 3
- Enhancement(rule-tester): Stricter rule test validations
- Enhancement(rule-tester): FlatRuleTester -> RuleTester
- Enhancement(rule-tester): detect duplicate test cases
- Enhancement(rule-tester): check for parsing errors in suggestion fixes
- Enhancement(rule-tester): check for missing placeholder data in the message
- Docs: Add blog post announcing v8 beta
- Docs: Consider using a URL shortener for links printed by code HOT 3
- Repo: Add a CI check that 'breaking change' PR descriptions start with the right title text
- Repo: Raise TSConfig lib/target support range to match ESLint's
- Enhancement: [return-await] Option for unopinionated "in-try-catch" HOT 4
- Bug: "Too many files (>8) have matched the default project" error triggers on every file after 8 HOT 7
- Docs: `method-signature-style` lint rule ❤ `--enforceReadonly` compiler flag
- Rule proposal: Disallow calling Number.isNaN on non-number types HOT 10
- Configs: Make consistent-type-imports 'error' for the recommended config HOT 24
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript-eslint.