TypeScript-DOM-lib-generator icon indicating copy to clipboard operation
TypeScript-DOM-lib-generator copied to clipboard

CustomEvent.detail is null when not defined

Open GauBen opened this issue 2 years ago • 3 comments

CustomEvent.detail is defined as follows:

interface CustomEvent<T = any> extends Event {
    readonly detail: T;

	// ...
}

Source: https://github.com/microsoft/TypeScript-DOM-lib-generator/blob/957eced9e98fc61bf86ab9524deb5eaa1f4d3c24/inputfiles/overridingTypes.jsonc#L538-L540

This is slightly wrong as CustomEvent.detail defaults to null when not defined: Playground

The playground contains a fix I suggest: detail: T extends {} ? T : null

I'll open a PR referring to this issue

GauBen avatar Jun 29 '23 07:06 GauBen

What about this code?

new CustomEvent("myEvent", {  detail: false });

Edit: I am surprised but this seems to work with your PR, too.

JFYI: This is easier to debug in playground with instant type highlighting:

let A: RealDetailType<undefined>
//  ^?
let B: RealDetailType<void>
//  ^?
let C: RealDetailType<null>
//  ^?
let D: RealDetailType<string | undefined> 
//  ^?
let E: RealDetailType<boolean> 
//  ^?

HolgerJeromin avatar Jun 29 '23 12:06 HolgerJeromin

I didn't know about the ^? trick, thanks @HolgerJeromin!

I'm not sure to understand the problem with detail: false, as false is defined, new CustomEvent("e", { detail: false }).detail === false

GauBen avatar Jun 29 '23 13:06 GauBen

I thought false would not qualify T extends {} so the result would be null instead of T. But, yes this worked. Sorry for the noise :)

HolgerJeromin avatar Jun 29 '23 13:06 HolgerJeromin