Skip to content

union with undefined not shown in type hint tooltip regression bugΒ #62325

Description

πŸ”Ž Search Terms

"type hints union undefined"

πŸ•— Version & Regression Information

  • This changed between versions 5.4.5 and 5.5.4

⏯ Playground Link

https://www.typescriptlang.org/play/?target=7&ts=5.5.4&ssl=10&ssc=1&pln=11&pc=1#code/C4TwDgpgBAShCOBXAlgJwgEwNIRAZwB4AVKCAD2AgDsM8oB7AIwCsIBjYAPigF4oBvAFBQRUANpYoyKlADWuegDNYCFOgzFOAXQBcUIhK0BuQQF8Tg0JCgAxevV4DBASACGAfj15gqaQHMTZ0ZPKCpEAFtGCFRAti8ff0CMPUZ7ABsIVxkAHyhEGghFaUwTU0FBAHoAKkEAUTJIDkwoKwg9S3BoACFXVEchUShXeN8qPyhc-IxC4owjKAICHj4pmapm5Dpwzbx-KRkAN2jd+hlOPgBWADprgBYoAApTtJB9loALaFaod+lgOmA6WAyDAAEphKJGHowpFohM8gUius5gslitEbMpFsdntpFAjqgTmdLjcrvcnlQXm9gJ8Wp0fn8AUCQeDBnEoN5RgEISJklBUvQMll4askSUzIIqhUOtYen0+HAkGhMDh8AQ7PROBYKhUoABhADyMBgtT1RGhDmiqHoqAANFAihA0hghkMaPyhugGGBgadXGlBGxTt4HfY9Br+jyoOyAOSMLIJmO2qN8nyICASyq6w3G03m0KW1DWu0O5BOl2uN0uxie6DoJXqfmIYAI6Zil2bIZpNL0ADumEDwZb8dQejlkcGw1ba0wycGUOn7bnolj8aoieXvL0aYzZSz+qNJrNeitNvtjudrqy1drUHragHQaoIZHACYx70Jyu9HGE+ukym26oOmEpAA

πŸ’» 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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions