Skip to content

type of Document.body getter should be nullable #44772

Closed
@trusktr

Description

@trusktr

body: HTMLElement;

Before the body is ready, the value is null.

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:

class Document {
  get body(): HTMLBodyElement | HTMLFramesetElement | null { ... }
  set body(value: HTMLBodyElement | HTMLFramesetElement) { ... }
}

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:

class Document {
  get body(): HTMLBodyElement | null { ... }
  set body(value: HTMLBodyElement) { ... }
}

but that still doesn't work.

Also, we could ignore the frameset deprecation and just write

class Document {
  get body(): HTMLElement | null { ... }
  set body(value: HTMLElement) { ... }
}

which also doesn't work.

This leads to runtime errors if someone is type-checking a <script> tag before body is ready.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs More InfoThe issue still hasn't been fully clarified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions