Comments (9)
That's not how type narrowing works. If the declared type is X
and CFA can determine that the type is also Y
, then the proper inference is that the type is X & Y
, not X | Y
. It must be both, not either/or.
And since we know that it is both HTMLAllCollection
and undefined
, the correct inference is never
.
Or are you suggesting that the typing of Document.all
be changed to undefined | HTMLAllCollection
?
from typescript.
@snarbies document.all
is a weird animal: because of spec weirdness for the purpose of not breaking the web, typeof document.all === 'undefined'
is true (as well as it being falsy and loosely equal to both null
and undefined
) despite it really being an HTMLAllCollection
.
from typescript.
@snarbies No, I'm saying that the given the information that typeof x === "undefined"
, the conclusion is only that x
has type undefined | HTMLAllCollection
and not that x
has type undefined
. This is because typeof document.all
is "undefined"
even though it has type HTMLAllCollection
.
from typescript.
fwiw I doubt TS is going to make a special case in the type checker specifically to account the weirdness of document.all
. This situation can't happen with any other value in JS and document.all
is deprecated anyway; MDN recommends against using it at all.
from typescript.
Yeah, I don't think this will be fixed. However I think it is nice to at least have an issue for every soundness issue.
from typescript.
I'm not sure I would even characterize this as a soundness issue. It's closer to magic.
There are scenarios that the type system simply can not convey, and this is true of most any type system. Magic, including exotics, by its very nature can and will defy the type system.
The only fix here would be to introduce type magic around the HTMLAllCollection
, which we can pretty safely assume wouldn't be happening.
from typescript.
A better fix would be to remove the type definition for document.all
entirely. Why would anyone use it in a Typescript project?
The only reason it still exists is to avoid breaking antiquated websites, but they most likely don't use Typescript anyway (or are not being maintained anymore).
from typescript.
document.all
is already marked deprecated.
from typescript.
This issue has been marked as "Won't Fix" and has seen no recent activity. It has been automatically closed for house-keeping purposes.
from typescript.
Related Issues (20)
- `tsBuildInfoFile` compiler option should be allowed without `incremental` or `composite` in Typescript versions `>5.6`
- super() typed as returning void HOT 2
- Move to File does not generate imports in the same way that autoimports does
- Move To File makes extra spaces when moving jsdoc comments
- Type all exported members according to some interface HOT 2
- JSDoc typed function comments inside functions
- Error when accessing CSS property value using kebab case in `CSSStyleDeclaration` object HOT 1
- Type 'never' incorrectly inferred for mixed primitive type properties assignment with strictNullChecks disabled HOT 2
- Array of object unions not working as expected HOT 2
- Support WebIDL interfaces nominally (not just structurally) HOT 9
- Improve type inference for nullish coalescing (??) after null checks on multiple variables HOT 2
- [NewErrors] 5.7.0-dev.20240915 vs 5.6.2 HOT 6
- [ServerErrors][JavaScript] 5.7.0-dev.20240915 vs 5.6.2 HOT 3
- [ServerErrors][TypeScript] 5.7.0-dev.20240915 vs 5.6.2 HOT 4
- Inlay parameter hints don't account for trailing required parameters HOT 1
- JsDoc with overloads and different generics results in wrong dts output
- Generic Conditional Type resolved to `any` when passed an error `any` type HOT 8
- Typescript v5.6.2 resulting in "Overload signatures must all be optional or required." on alpine linux arm64 HOT 2
- File watching is registering thousands of redundant watchers
- File watching creates hundreds of recursive watchers in mono-repo when opening loose file
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.