-
Notifications
You must be signed in to change notification settings - Fork 12.8k
type of Document.body getter should be nullable #44772
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
Comments
Here's a trick to make it work: class Document2 extends Node {
// @ts-expect-error
get body(): HTMLBodyElement | null {
return {} as any
}
set body(el: HTMLBodyElement) {
}
}
const b: HTMLBodyElement = new Document2().body // error (good), getter can return null
new Document2().body = null // error (good), setter accepts only non-null |
Are you asking for #43662 in general, or are you actually asking for the type of |
I don't understand why a getter that returns |
I’m totally sympathetic to that reasoning and we’ve seen a number of examples from the DOM that can’t be described by today’s variant accessor implementation. I’d encourage both of you to post examples like these in #43662. |
@andrewbranch Hello, thanks for the reply. Indeed, I am not asking about #43662, but just noting that the actual type of But related to #43662, as @fatcerberus mentioned, it is not currently possible to denote such a type in TS. I think I would just make it |
Or, if Based on the fact that the |
Feel free to start a discussion at https://github.com/microsoft/TypeScript-DOM-lib-generator, but I am sticking with my analysis that this would be
|
TypeScript/lib/lib.dom.d.ts
Line 4453 in 3b2bc85
Before the
body
is ready, the value isnull
.PR: #44773
Also, although TypeScript now has ability for a getter to have a different type than a setter, it is still not possible to make the type properly. Based on Chromes behavior this is what describes it:
however such a type is not allowed in TS 4.3.
If we follow the deprecation of
HTMLFramesetElement
, which I think is a good idea, then the type would be this:but that still doesn't work.
Also, we could ignore the frameset deprecation and just write
which also doesn't work.
This leads to runtime errors if someone is type-checking a
<script>
tag beforebody
is ready.The text was updated successfully, but these errors were encountered: