Repository navigation
Conversation
Spreading the result of a generic function call into a styled component fails with TS2698 since callstack#1479 added the generic `as` overload. TypeScript does not infer such a spread against that overload, so add a non-generic overload without `as` first. The `as` overloads are unchanged.
🦋 Changeset detectedLatest commit: 6d6708b The changes in this PR will be included in the next version bump. This PR includes changesets to release 9 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Since
@linaria/react8.1.0 (#1479, for #1414), spreading the result of a generic function call into a styled component fails to type-check:react-dropzonetypes it as:The same code compiles with
@linaria/reactv7 and up to v8.0.0, and with a plain<input>. Hoisting the call into aconstbefore the JSX also works, but it is easy to hit and the error doesn't point at the cause.Reproduced it with
react-dropzonev14 and v20. ThegetInputPropssignature is the same in both.Summary
#1479 changed
StyledComponent<T>from a plainFunctionComponentinto an overloaded call signature whose first overload is generic. TypeScript cannot infer a spread of a generic call against that overload. A standalone file with only the two call-signature shapes (no Linaria) shows the same split.This PR adds a non-generic overload without
asin front of the existing ones:The
asoverloads are unchanged, so #1414's typing is preserved.How styled-components types the same thing
@types/styled-componentsv5 has two call signatures. The first is non-generic and takesas?: never | undefined, and the second is the generic<AsC …>one. That is the same structure as here: index.d.ts#L137-L151.<AsTarget … = void, ForwardedAsTarget … = void>(props: PolymorphicComponentProps<…>). types.ts#L213-L221.Test plan
ComponentProps,Parameters, assignability toComponentType,ElementTypeandFC,aspointing at a custom component,styled(StyledThing)andstyled(CustomComponent), refs and event handlers, components with required own props, and spreads of plain objects.