Skip to content

Removing 'optional' attributes from a type's properties disallow adding undefined as it's type #46850

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

Closed
KuSh opened this issue Nov 18, 2021 · 7 comments
Labels
Unactionable There isn't something we can do with this issue

Comments

@KuSh
Copy link

KuSh commented Nov 18, 2021

Bug Report

πŸ”Ž Search Terms

type generic remove optional attribute

πŸ•— Version & Regression Information

I did test this on 4.4.4 and 4.5.0-beta using playground

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type NullableType<T> = {
    [K in keyof T]-?: undefined extends T[K]
    ? T[K] | null | undefined
    : T[K]
}

πŸ™ Actual behavior

Removing optional attribute from type also remove undefined from final type
(when not removing optional attribute, final type is x?: typeof x | null | undefined)

πŸ™‚ Expected behavior

Removing optional attribute just removes the optional attribute and doesn't modify final type

@MartinJohns
Copy link
Contributor

Sounds like a duplicate of #31025.

@RyanCavanaugh RyanCavanaugh added the Unactionable There isn't something we can do with this issue label Nov 18, 2021
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Nov 18, 2021

I don't see any repro here that demonstrates what you're talking about. Please provide an minimal/actionable sample that clearly demonstrates what you consider to be the bug

@KuSh
Copy link
Author

KuSh commented Nov 19, 2021

Hi @RyanCavanaugh,

Hover over BadNullableTest in the playground, you'll see that previously optional attributes are defined as string | null instead of string | null | undefined

Regards,
Nicolas

@RyanCavanaugh
Copy link
Member

Please provide an minimal/actionable sample that clearly demonstrates what you consider to be the bug, not a textual description of where to look in a long sample

@tadhgmister
Copy link

@KuSh does the relatively new compiler option exactOptionalPropertyTypes make it behave the way you are expecting? playground

type NullableType<T> = {
    [K in keyof T]-?: undefined extends T[K]
    ? T[K] | null | undefined
    : T[K]
}

type Test = {
    req: string
    opt?: string
    canBeNull: string | undefined
}

type EQ = NullableType<Test>
//   ^?
const shouldPass: EQ = {req: "", opt: undefined, canBeNull: undefined}

@KuSh
Copy link
Author

KuSh commented Nov 22, 2021

@KuSh does the relatively new compiler option exactOptionalPropertyTypes make it behave the way you are expecting?

It seems so. But is effectively too recent for my current project (which use TS 4.3) and also has other consequencies as we're used to set optional props as undefined (outside of NullableTypes). Thanks for tip I'll keep that in mind πŸ‘

@github-actions
Copy link

github-actions bot commented Jun 8, 2023

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

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Unactionable There isn't something we can do with this issue
Projects
None yet
Development

No branches or pull requests

4 participants