Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

value of type never can be created using document.all #58502

Closed
tyilo opened this issue May 11, 2024 · 9 comments
Closed

value of type never can be created using document.all #58502

tyilo opened this issue May 11, 2024 · 9 comments
Labels
Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it

Comments

@tyilo
Copy link

tyilo commented May 11, 2024

πŸ”Ž Search Terms

  • document.all
  • HTMLAllCollection

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Common "Bugs" That Aren't Bugs

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.4.5#code/GYVwdgxgLglg9mABMAFANwFyLAUzTgJwEos04YATRAbwChEHEIEBnOAGxwDp24BzFAAMAygAs4IdhTAByKIgBGORAAc4LFjAWcMggDSI0RANy0AvrVrMwLeQA9EAXkQU4EEAFscYKFwCG7OymMMCIKFAAnio4cKEOjgmIMuAUOMAwuBQyRDT0jKh2JuZAA

πŸ’» Code

function f(v: never): void {
    console.log(`Shouldn't be possible:`, v);
}

const x = document.all;
if (typeof x === 'undefined') {
    f(x);
}

πŸ™ Actual behavior

The code type checks and f is called at runtime even though it has a parameter of type never.

πŸ™‚ Expected behavior

The type of x should not be narrowed have type undefined when typeof x === 'undefined' has been established.
Instead it's type should be narrowed to undefined | HTMLAllCollection.

Additional information about the issue

No response

@snarbies
Copy link

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?

@fatcerberus
Copy link

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

@tyilo
Copy link
Author

tyilo commented May 13, 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.

@fatcerberus
Copy link

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.

@tyilo
Copy link
Author

tyilo commented May 13, 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.

@snarbies
Copy link

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.

@guillaumebrunerie
Copy link

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

@RyanCavanaugh RyanCavanaugh added the Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it label May 14, 2024
@RyanCavanaugh
Copy link
Member

document.all is already marked deprecated.

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Won't Fix" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it
Projects
None yet
Development

No branches or pull requests

6 participants