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
Allow all keys to be optional in the JsonObject
type
#65
Conversation
Can you add some type tests? |
@sindresorhus Yup I can. But, before that, I need to know whether these changes look good :) Otherwise, the tests will be useless :P |
@SamVerschueren Is this what you had in mind? |
Just wondering, but |
@yxliang01 Why did you change it to |
@sindresorhus It was simply that |
I'm not aware of the difference either. One problem with either of these solutions is that it will allow: interface Unicorn extends JsonObject {
rainbow: undefined;
} |
So, the intended behavior is that all keys can be optional while it is a must that we can't make the value The technical difficulty to achieve the intended semantic is that the TypeScript type system doesn't differentiate the following cases:
Note that these two cases are different from the JavaScript perspective. Thus, it might be impossible to achieve our goal with current TypeScript type system? |
@resynth1943 Any thoughts on this? |
I'll take a look and see what I think. |
Related: microsoft/TypeScript#13195 |
Apologies for the late reply. I don't really see any major problems with this regarding serialized and de-serialized JSON types, as keys could be optional in that context. LGTM. |
@yxliang01 Could you please follow the instructions in this document? |
JsonObject
type
Finally merged after created for 4 months 🎉 |
Yay! |
By right, when an object has certain keys optional, the object is still a valid
JsonObject
. So, it makes more sense to allow all keys to be optional. Indeed, anJsonObject
doesn't always have values for all string indexes.This resolves #64 .