Comments (6)
It looks like this is the related TypeScript bug:
from typescript-eslint.
Both members of the intersection have flag?: …
, not flag: …
. So the property is allowed to be absent.
Which is why you can write options.flag.valueOf() without any guards and TS will not error.
This does seem like a TypeScript bug.
When we inspect the types for .flag, TS tells us its type is just boolean. It says there's no possibility of it being undefined. Hence the rule errors.
That shows (property) flag?: boolean
. The ?
indicates that TypeScript knows the property might be absent.
from typescript-eslint.
That shows (property) flag?: boolean
Yes - that's because TS's "intellisense printer" shows you the type AND the context. So when it shows you the context (the property name) it also needs to show the "divider". When it prints the divider it also inspects the flags for the property to determine if it should also print the ?
in addition to the standard :
.
microsoft/TypeScript#58174 looks to be the culprit - that's why it works without the intersection but breaks it with it.
So I'll close this as a bug in TS.
from typescript-eslint.
This is actually working as expected.
When you intersect two union types the resulting type is the intersection of the unions - i.e. the types that exist in both unions.
For example - type T = (number | undefined) & number;
- T === number
.
When you intersect two object types TS will include all the unique properties between the two types, and then it will intersect the common properties. So what you end up with in your example is boolean & (boolean | undefined)
- i.e. boolean
.
Which is why you can write options.flag.valueOf()
without any guards and TS will not error.
When we inspect the types for .flag
, TS tells us its type is just boolean
. It says there's no possibility of it being undefined
. Hence the rule errors.
If you tweak the types slightly you'll see that rules like no-unnecessary-condition
will also error here because the types tell us there's no undefined
.
IDK if the behaviour is correct from TS's side here - surely it should be erroring on the property / saying the type is undefined
at read time?
I don't know for sure - would require an issue to discuss with them.
from typescript-eslint.
@bradzacher but granted it's an optional property the two intersection parts are basically identical unless you have the --exactOptionalPropertyTypes
flag right?
FWIW our code just doesn't handle intersections at all, see
Also, side note, this seems like the type of case that would benefit from assignability checking a la microsoft/TypeScript#56448. Is there an issue on our side for introducing that into the codebase now that it's landing/landed?
from typescript-eslint.
Disregard my comment, didn't realize --exactOptionalPropertyTypes
is set
from typescript-eslint.
Related Issues (20)
- Repo: publishing v8 canary versions failed - "Not Found - PUT https://registry.npmjs.org/@typescript-eslint/types - Not found" HOT 2
- Bug: Broken examples? HOT 1
- Docs: Quickstart guide should be clear on type: module not being a requirement HOT 6
- Repo: Failing to publish new alpha versions of v8 post-v8.0.0-alpha.10 HOT 2
- Bug: [v8.0.0-alpha.10] incorrect error message `Parsing error: "parserOptions.programs"` HOT 1
- Bug: [member-ordering] Not Detecting Order For Read-Only Fields Correctly HOT 1
- Bug: Default configuration following https://typescript-eslint.io/getting-started results in error HOT 1
- Bug(typescript-estree): always parse mts/mjs as ESM for non-type-aware parsing HOT 6
- Docs: Write blog post on the history of ban-types, {}, and object/Object
- Rule proposal: no-unsafe-typeof-function HOT 5
- Types: `plugin` and `parser` types incompatible with ESLint types HOT 6
- Issue with the subDependencies of typescript-eslint HOT 1
- Bug: Type incompatibility with `fixupPluginRules()` from `@eslint/compat` HOT 3
- Bug: parsing a function with argument throws an error when `range` parse option is not set HOT 6
- Bug: [8.0.0-alpha] [no-unused-vars] Support for `ignoreClassWithStaticInitBlock` and `reportUsedIgnorePattern` Options HOT 7
- Bug: "Invalid string length" caused by allowDefaultProjectForFiles warning HOT 4
- Enhancement: [no-inferrable-types] Support for the upcoming compiler options `isolatedDeclarations` HOT 2
- Bug: typescript eslint raises warnings when used with react-hooks HOT 6
- Repo: include github tags in releases HOT 4
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.