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.

Optional without undefined union

See original GitHub issue

Sorry 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
colinhackscommented, Jul 16, 2021

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:

type Foo = {
  bar?: string;
};

type id<T> = T;
type NewFoo = id<Foo>; // { bar?: string | undefined }
0reactions
scotttrinhcommented, Oct 6, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

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