Giter VIP home page Giter VIP logo

Comments (9)

snarbies avatar snarbies commented on September 23, 2024

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.

fatcerberus avatar fatcerberus commented on September 23, 2024

@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.

tyilo avatar tyilo commented on September 23, 2024

@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.

fatcerberus avatar fatcerberus commented on September 23, 2024

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.

tyilo avatar tyilo commented on September 23, 2024

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.

snarbies avatar snarbies commented on September 23, 2024

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.

guillaumebrunerie avatar guillaumebrunerie commented on September 23, 2024

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.

RyanCavanaugh avatar RyanCavanaugh commented on September 23, 2024

document.all is already marked deprecated.

from typescript.

typescript-bot avatar typescript-bot commented on September 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.