Removing 'optional' attributes from a type's properties disallow adding undefined as it's type
See original GitHub issueBug 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:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sounds like a duplicate of #31025.
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 👍