-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed as not planned
Closed as not planned
Copy link
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
π Search Terms
"type hints union undefined"
π Version & Regression Information
- This changed between versions 5.4.5 and 5.5.4
β― Playground Link
π» Code
type RequiredKeys<T extends object> = {
[K in keyof Required<T>]: T[K];
};
type Foo = {
a?: string;
b?: number;
c: string;
d: boolean | undefined;
}
/*
Expected type:
type Bar = {
a: string | undefined; <<== undefined is missing in version >= 5.5.4 (only in the type hints tooltip)
b: number | undefined; <<== undefined is missing in version >= 5.5.4 (only in the type hints tooltip)
c: string;
d: boolean | undefined;
}
*/
type Bar = RequiredKeys<Foo>;
// CORRECT: no error, field a and b are optional
const foo: Foo = {
c: 'banana',
d: true
}
// CORRECT: no error, field a and b are required but undefined is allowed
const bar: Bar = {
a: undefined,
b: undefined,
c: 'banana',
d: true
}
// CORRECT: error, field a and b are required
const bar2: Bar = {
c: 'banana',
d: true
}π Actual behavior
The purpose of RequiredKeys is to make all optional fields required, but leave the union with undefined intact.
The actual behavior is that it also removes the union with undefined, but only in the type hint tooltip, the compiler and type instances do work.
This issue started from version 5.5.4 and up.
This is the incorrect type in the type hint tooltip, fields a and b should have a union with undefined:
type Bar = {
a: string;
b: number;
c: string;
d: boolean | undefined;
}π Expected behavior
It's expected that the type hint tooltip shows the union with undefined for fields a and b.
This is the correct type in the type hint tooltip, fields a and b have a union with undefined:
type Bar = {
a: string | undefined;
b: number | undefined;
c: string;
d: boolean | undefined;
}Additional information about the issue
Toggle between versions 5.4.5 and 5.5.4 in the playground and hover over the Bar type to see the difference.
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created