Skip to content
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

Proposal: SetKeyRequired with key required, but allow undefined value #856

Closed
Clindbergh opened this issue Apr 4, 2024 · 2 comments
Closed

Comments

@Clindbergh
Copy link

Clindbergh commented Apr 4, 2024

type a = { a?: number, b: number | undefined};

type b = SetRequired<a, 'a' | 'b'>;

I would have expected the type b to be: (1)

{ a: number, b: number | undefined }

however the type seems to be

{ a: number, b: number }

Is there a way to enforce the presence of the key but not require the value to be defined (like (1))?

This can be manually achieved with this type definition:

{ a: b['a'], b: b['b'] }

But this is a bit more cumbersome.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@sindresorhus
Copy link
Owner

Do you have "exactOptionalPropertyTypes": true, in your tsconfig?

@Clindbergh
Copy link
Author

Thanks a lot @sindresorhus for drawing my attention on exactOptionalPropertyTypes which was unknown to me.
For anyone who also thought strict is the strictest you can get, I found this article quite helpful.

At the moment I am unable to reproduce the issue, I may have been working with an older typescript version. It seems to work as expected. exactOptionalPropertyTypes should not be required. Closing it for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants