Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-? (or Required<T>) swallows undefined during mapped types #60255

Open
entropitor opened this issue Oct 17, 2024 · 1 comment
Open

-? (or Required<T>) swallows undefined during mapped types #60255

entropitor opened this issue Oct 17, 2024 · 1 comment

Comments

@entropitor
Copy link

πŸ”Ž Search Terms

swallows undefined -? mapped types

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.0-beta&ssl=15&ssc=1&pln=17&pc=1#code/C4TwDgpgBAKhDOwbmgXigbygezMAltgHYCGANgGL4RkAmA-AFxSIBO+RA5gNxRisIIRAMYQAShACOAV3wDaAIWnAAwiSIKIAVSK0IAMw4RazItIC2AIwisoAHyjTdBo7V4CZc480vZsZCHUoAF8AKFDQSCgAQTIAdxIQeABlYHYuAHlWHT1DImMAHhgAPih0DFCoKABtAGkIECgOKABrBux9KAlPeSLigF1mNg5Oe0dnPONuUODpyOgYAEYymPjElLSRrJyXfNoihCQUYtCAelOqqAA9ejOLq5w8QlJKajooAAsSeCgAK2lEFAAAbDLhAgA0UCI2HGuVc4XmqwSSVS6U42wmriyADlpGQyH0VhUqnUGk0iK12p1urJeiVBixNlwxk44XsxmZ8dNZhEULBFpYVrFkRs0Ri2YU4IhkJATudLjc7tdHgRiOQqDRaJ9vn8AcBgaDOBCoTDWbtjOFeVEAOr4YAfWKvTXwACiAA8wGR8MI7YTypUavVGs02iAOl0pLTJQNmBlzL6YJCg6UIG7gEJaD8YFB6LBSSB+hy8WQoMwYPn+ty5nyYAAmFa2+2OjV0V0er0+4AHaXHabyqqK+UACRs0DtUDi2FYLR++E6YekUBItC1ACIHJyyKvIfahFBd7DzVqSGBICRWD8SJwSBwAHT3oA

πŸ’» Code

type TestType = { optionalField?: string; presenceRequiredButCanBeUndefined: number | undefined; required: boolean }

type AlwaysStringOrUndefined<T> = {
  [Key in keyof Required<T>]: string | undefined;
};
type T1 = AlwaysStringOrUndefined<TestType>
//   ^?
// ^ optionalField has just `string`, no undefined

type AlwaysStringOrUndefinedOrNull<T> = {
  [Key in keyof Required<T>]: string | undefined | null;
};
type T1b = AlwaysStringOrUndefined<TestType>
//   ^?
// ^ optionalField has just `string`, no undefined


type WithAllFieldsExplicit<T> = {
  [Key in keyof Required<T>]: Omit<T, Key> extends T ? T[Key] | null : T[Key];
};
type T2 = WithAllFieldsExplicit<TestType>;
//   ^?
// Here it works if you add "| null", then the undefined appears again... If you just do `| undefined` it gets swallowed

πŸ™ Actual behavior

The | undefined gets swallowed from mapped types when mapping from a type with optional fields which are removed through Required<T> or through -?

πŸ™‚ Expected behavior

The undefined should not be swallowed. Especially not when using keyof Required<T> instead of -?

Additional information about the issue

No response

@jcalz
Copy link
Contributor

jcalz commented Oct 17, 2024

Duplicate of #31025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants