Optional without undefined union
See original GitHub issueSorry if this is an ignorant question but is it possible to create an object with an optional key without the | undefined
?
i.e. this
type Foo = {
bar?: string;
}
instead of the
type Foo = {
bar?: string | undefined;
}
You get when using .optional()
.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top Results From Across the Web
optional vs. undefined - TkDodo's blog
There is a subtle difference between optional fields and required, but potentially undefined ones.
Read more >When to use typescript optional "?" parameter vs explicit ...
The most important thing to understand is that “?” optional property and same property declared as required but possibly undefined mean ...
Read more >Typescript: What's the difference between an optional field ...
I thought the following were synonymous. (A) : interface State { userDetails: undefined | string | DataStructure | Error ...
Read more >How to Deal with Optional Things and "Undefined" in TypeScript
Working with JavaScript means working with undefined . It's a standard way to say, “This thing you asked for doesn't exist.”.
Read more >no-null-undefined-union - Rule
Rule: no-null-undefined-union. Disallows explicitly declared or implicitly returned union types with both null and undefined as members.
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
Nope that’s not possible. They’re treated identically by TypeScript so there’s no reason for it. You can see that the
| undefined
gets automatically added by TS during any operation on the type, even trivial ones:Yeah,
exactOptionalPropertyTypes
is a pretty new feature, since it came out in 4.4. Might take some time to implement and require some care. I feel like maybe we discussed having it as a separate method to avoid breaking backwards compatibility, but I can’t seem to find the issue now.