Skip to content

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

Closed as not planned

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 }>>>

Activity

  1. jcalz commented on Nov 4, 2024

    @jcalz
  2. verytactical commented on Nov 4, 2024

    @verytactical
    Author

    Joe Calzaretta (@jcalz) This is a different issue. Here it's not only removed, it's impossible to even set it afterwards.

  3. jcalz commented on Nov 4, 2024

    @jcalz
  4. verytactical commented on Nov 4, 2024

    @verytactical
    Author

    removing undefined is happening at the end of the type manipulation

    Honestly I would never expect that. Thanks for explaining!

    It's probably still related to #31025

    It's definitely related, because if undefined was never removed along with optionality, it wouldn't be removed at incorrect time :)

  5. jcalz commented on Nov 4, 2024

    @jcalz
  6. Andarist commented on Nov 4, 2024

    @Andarist
    Contributor

    I believe this is just a display issue as this works alright:

    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 }>>;
    
    const theWhat: TheWhat = {
      a: undefined, // ok
    };

    It's likely a duplicate of #59948 and likely is being fixed by #59957 // EDIT: yep, I just pushed a test case there based on this issue

  7. jcalz commented on Nov 4, 2024

    @jcalz
    Contributor

    Okay, okay this has nothing to do with #31025 then. Sorry! I'll hide my comments (including this one, soon) to cut down on noise.

  8. typescript-bot commented on Nov 7, 2024

    @typescript-bot
    Contributor

    This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  9. locked as resolved and limited conversation to collaborators on Oct 22, 2025
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