Open trusktr opened 1 week ago
Isn't this a duplicate of your own #43729?
Ah yes indeed, I forgot, and search failed. 😄 This one details the JSX used case better (which was mentioned in https://github.com/microsoft/TypeScript/issues/43729#issuecomment-2315915614).
I closed
in favor of this one.
🔍 Search Terms
typescript extract setter types mapped type
✅ Viability Checklist
⭐ Suggestion
(original thread on Discord)
This might be a request for a new utility type, or a new language feature, because there seems to be no way to do it with current language features currently:
Given this type:
We want to derive a type like this:
There seems to be no way to implement
PickWithSetterTypes
in userland.📃 Motivating Example
When writing class-based JSX components (for example, custom elements are written as classes), the JSX property types are setters, not getters.
In this JSX expression:
The
foo
prop is asetter
, and it is setting the property on the custom element, it is not reading the property from the element.So, when we define a class component, for example:
We need to pluck the properties that we want available in the JSX. For example, something along the lines of this:
Now, the problem is, when we try to set a valid value for the property in JSX, it will not work:
There should not be a type error, because the setter actually does accept the value
'foo'
.💻 Use Cases
What workarounds are you using in the meantime?
Workarounds could include providing separate named properties that can be extracted with template string types, but it is very cumbersome
With this method, now a utility type can be written that can use template string types to extract the setter type from the non-setter dummy property type, something like this:
TypeScript playground example