Use inference for filling JSX attributes in getJsxElementInstanceType#20973
Conversation
sandersn
left a comment
There was a problem hiding this comment.
Looks good, a few cleanup items.
| @@ -16,11 +20,15 @@ tests/cases/conformance/jsx/file.tsx(17,18): error TS2559: Type '{ a: string; }' | |||
|
|
|||
| // OK: we fille in missing type argument with empty object | |||
There was a problem hiding this comment.
need to update comments here too (or move around the test cases I guess)
| internalProp: P; | ||
| } | ||
|
|
||
| // Error |
| * | ||
| * @param openingLikeElement a non-intrinsic JSXOPeningLikeElement | ||
| * @param shouldIncludeAllStatelessAttributesType a boolean indicating whether to include all attributes types from all stateless function signature | ||
| * @param sourceAttributesType Is the attributes type the user has actually passed, and is used to create interfences in the target type if present |
There was a problem hiding this comment.
delete "has actually"
typo "inferences"
There was a problem hiding this comment.
As usual, I'm not convinced these @param tags are useful. The @return is, though.
Well, the second sentence for elementType is useful, I guess.
| // attr1 and attr2 are treated as JSXAttributes attached in the JsxOpeningLikeElement as "attributes". | ||
| const sourceAttributesType = createJsxAttributesTypeFromAttributesProperty(openingLikeElement, checkMode); | ||
|
|
||
| // targetAttributesType is a type of an attributes from resolving tagName of an opening-like JSX element. |
There was a problem hiding this comment.
sourceAttributesType is now used in the targetAttributesType assignment expression; it's not just a code move.
|
|
||
| function inferJsxTypeArguments(signature: Signature, sourceAttributesType: Type, context: InferenceContext): Type[] { | ||
| // Clear out all the inference results from the last time inferTypeArguments was called on this context | ||
| for (const inference of context.inferences) { |
There was a problem hiding this comment.
when is a context reused? I only see inferJsxTypeArguments being called right after creating a fresh context.
There was a problem hiding this comment.
Good point. It may be possible to reuse them in some way in the future, but I'm not doing so now.
Technically, stateless functional components do do an inference pass internally during overload resolution, but
checkJsxAttributesAssignableToTagNameAttributesdoesn't use those results, since its agnostic to the component kind, but needs to have a correctly instantiated type in all cases so inverted variance positions can be handled correctly in its assignability check.Fixes #20891