question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
MartinJohnscommented, Nov 18, 2021

Sounds like a duplicate of #31025.

0reactions
KuShcommented, 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 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to omit optional properties from type? - Stack Overflow
My current understanding why it should work is the following: Required<T>[K] turns all optional fields of T to non optional, then if the...
Read more >
How to Deal with Optional Things and "Undefined" in TypeScript
First, if you don't tell TypeScript that a property is optional, it will expect it to be set. Adding ? to the property...
Read more >
How to Convert Object Props With Undefined Type to Optional ...
First I thought about mapped types as we can add or remove optional modifiers in mapped types. Sadly, this won't work since we...
Read more >
Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
operator accesses an object's property or calls a function. If the object accessed or function called is undefined or null, ...
Read more >
Documentation - Advanced Types - TypeScript
Property 'fly' does not exist on type 'Fish | Bird'. ... eliminate null or undefined , you can use the type assertion operator...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found