Fixing react defaultize+generic default props interaction#27088
Fixing react defaultize+generic default props interaction#27088weswigham merged 4 commits intomicrosoft:masterfrom
Conversation
|
Plus side, this should probably fix the |
| !!! error TS2322: Type '(a: { x: string; }) => string' is not assignable to type '(cur: { x: string; }) => { x: string; }'. | ||
| !!! error TS2322: Type 'string' is not assignable to type '{ x: string; }'. | ||
| !!! related TS6500 tests/cases/conformance/jsx/file.tsx:13:54: The expected type comes from property 'nextValues' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<GenericComponent<{ initialValues: { x: string; }; nextValues: {}; }, { x: string; }>> & { initialValues: { x: string; }; nextValues: {}; } & BaseProps<{ x: string; }> & { children?: ReactNode; }' No newline at end of file | ||
| !!! error TS2322: Type '(a: { x: string; }) => string' is not assignable to type '((a: { x: string; }) => string) & ((cur: { x: string; }) => { x: string; })'. |
There was a problem hiding this comment.
This intersection is unfortunate. Is there a way to avoid adding (a: { x: string }) => string to the type?
There was a problem hiding this comment.
The target is Props & BaseProps<Values> where we've already infered Props as { initialValues: {x: string}; nextValues: (cur: {x: string}) => string; } (since that's the entire attributes type as written, which is inferred to Props) and BaseProps<Values> as interface BaseProps<T> { initialValues: T; nextValues: (cur: T) => T; } instantiated with T = Values = {x: string} (since that's what first matches T in the first context-free inference pass) - the intersection of both's nextValues member is the type we see here. Everything is as it should be.
sandersn
left a comment
There was a problem hiding this comment.
Looks good, although might be good to follow up on the one test comment I had.
Specifically, this fixes where the contextual type is places for self-closing tags (it should be on the attributes since there's no children to wrap, and definitely not the expression the tag is within), and fixes propagating flags for jsx attributes types (previously they propagated none, which wasn't a big deal before we introduced inference, but now they need to propagate flags correctly so they propagate the
ContainsAnyFunctionTypeflag to prevent spurious inference results during the first pass).Fixes #26395
I've also updated our test harness react.d.ts with a newer one, since it's adopted many features since the version currently present. Right now it's s separate file from the old one, but we probably need to be validating our tests against the new .d.ts; however there's ~100 tests to look through once you make the swap, which'll take a bit; I'd like to save that for a follow-up.