Skip to content

Fields have an invisible "never make it undefined" stateΒ #60411

@verytactical

Description

@verytactical

πŸ”Ž Search Terms

undefined forever

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAqgdgZwseATCAzAlnCqAqA9gPIBGANlgG5aFwA8+AfFALxQDeUA2gApQ4oAawghCGKPgC6AWgD8ALkl8pUAD5QArnHTZcqANxQAvlABQoSFADKyNJhx5GLdlz4C4w0eMlSl+FXUtHQd9E3NLaHwACwgAdWiAQ2A2GzsQvSd4JBQMxwISCmpaBi5ExSgARhMmJjMgA

πŸ’» Code

type UnsetUndefinedToOblivion<T> = { [P in keyof T]-?: T[P] | undefined; } 
type SetUndefined<T> = { [P in keyof T]: T[P] | undefined } 
type TheWhat = SetUndefined<UnsetUndefinedToOblivion<{ a?: 1 }>>

πŸ™ Actual behavior

type TheWhat = {
    a: 1;
}

πŸ™‚ Expected behavior

type TheWhat = {
    a: 1 | undefined;
}

Additional information about the issue

Temporary fix:

type UnsetTheUnset<T> = { [P in keyof T & {}]: T[P] }
type TheWhat = SetUndefined<UnsetTheUnset<UnsetUndefinedToOblivion<{ a?: 1 }>>>

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