Allow different properties between the JSX expression and component type #55248
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Suggestion
π Search Terms
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Concept based on #14729
Introduce a new function to the
JSX
namespace calledconvertProperties
(subject to change) which takes a component and returns an object representing its JSX props.When the typechecker sees a component expression (e.g.
<MyComponent prop={val} />
), it will determine the types of the required properties by performing a virtual call toJSX.convertProperties
with the provided component type and using the return type. For example:π Motivating Example
The Solid framework is very similar to React, but rather than tracking reactive dependencies with an explicit dependencies array like in React's
useMemo
anduseEffect
, Solid requires calling a function to retrieve the value of a piece of state, allowing for implicit dependency tracking based on which states were previously fetched the last time the memo/effect callback ran (see https://www.solidjs.com/guides/reactivity#introducing-primitives). It is a similar concept to Knockout's observables.However, when it comes to component properties, Solid uses proxy dereferences instead of explicit function calls. This leads to a common footgun where destructuring the
props
object like one might do in React will prevent dependencies from being tracked (https://www.solidjs.com/guides/faq#why-do-i-lose-reactivity-when-i-destructure-props). An alternative design could have had the jsx factory function wrap every prop in a reactive getter before bundling them into theprops
object.By separating the prop types between the component creation and the JSX where it's used, TypeScript would be able to support such a design in a new web framework.
π» Use Cases
See above.
Another similar use case could be:
This feature would also allow performing the reverse operation of
JSX.IntrinsicAttributes
. For example:Alternative Designs
Alternatively, a solution which is slightly less powerful but likely just as useful in 90% of cases could be to declare generic types on the
JSX
namespace like is done withJSX.IntrinsicClassAttributes<T>
:The text was updated successfully, but these errors were encountered: