-
Notifications
You must be signed in to change notification settings - Fork 12.8k
optional and readonly Filter Modifier for keyof #35103
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
Comments
Related to #31581 Relevant SO question. Bikeshedding: I think |
Thanks @jcalz , somehow I missed that So topic when I googled about this. The '+' / '-' makes sense, in my mind they were more like "add that modifier" and "remove that modifier" than "does it have that modifier?". Will add that to my question. |
[1] Here's a relevant SO answer for Optional/Non-Optional filtering. [2] I think there's one more combination:
Edit: Silly me; of course 2x2 => 4 combinations:
[3] With respect to +/-, they do mean add/remove. I think it may be confused with this existing usage (which means "make writable" not "is writable?"):
|
An alternative syntax proposalSince
A more explicit opposite of readonly is "writable":
I find this much more readable, especially when you consider combinations:
|
Looking for this, |
This would be really useful |
Uh oh!
There was an error while loading. Please reload this page.
Search Terms
Suggestion
Adding Support for modifiers to keyof could be very helpful. I'm picturing something like this
The modifiers would act as "filters", only selecting the keys fitting the given modifiers.
As @jcalz pointed out, it might make more sense to use the existing
+
and-
operators instead of the!
.Use Cases
This could be really useful for selecting properties of types based on them being
optional
/readonly
. A possible use case for this would be the ReactdefaultProps
object (see example).Of course this could negatively affect readability as especially Mapped Types can get fairly long
but I think it's still in the realms of comprehensibility.
Examples
Especially with many optional props the current method of
can get fairly tedious, especially as you have to add new optional props at 3 places (the type definition, the
Pick
UtilityType and in thedefaultProps
). The method using PickOptional and the?
-keyof
-modifier reduces it to 2, and immediately notifies you if you declared a parameter optional, but haven't defined it indefaultProps
.This would also work without React in terms of default values:
I'm sure there are more (and better) use cases for this feature I can't think of right now...
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: