Generics support for typescript
See original GitHub issueWhen creating dynamic components we often use generics syntax to define types for props.
We’re doing it like so:
const MyComponent = styled<SomeProps>`
color: ${(p) => p.isActive ? 'red' : 'blue'};
`
const MyOtherComponent = MyComponent.extend<OtherProps>`
color: ${(p) => p.isFocused ? 'red' : 'blue'};
`
Yes, vscode-styled-components
supports some of the generics, but with current support it’s impossible to extend extend other components, only built-in elements because this following will not pass TS type validation:
const MyComponent = styled<MyProps>(NotAStyledComponent)`
color: ${(p) => p.isActive ? 'red' : 'blue'};
`
// these two also will cause validation error
const MyComponent = styled<MyProps, NotAStyledComponent>(NotAStyledComponent)`
color: ${(p) => p.isActive ? 'red' : 'blue'};
`
// even if styled component passed
const ComponentOne = styled.div`
background-color: #fff;
`
// both will fail
const MyComponent1 = styled<MyProps, ComponentOne>(ComponentOne)`
color: ${(p) => p.isActive ? 'red' : 'blue'};
`
const MyComponent1 = styled<MyProps>(ComponentOne)`
color: ${(p) => p.isActive ? 'red' : 'blue'};
`
We sure can do something like this:
const MyComponent1 = styled<MyProps, any>(ComponentOne)`
color: ${(p) => p.isActive ? 'red' : 'blue'};
`
But it will eventually force us to use any
in all underlying code or will break its validation otherwise.
It would be great if you’ll introduce generics support 😃
Issue Analytics
- State:
- Created 5 years ago
- Reactions:77
- Comments:10 (1 by maintainers)
Top Results From Across the Web
Documentation - Generics - TypeScript
A generic class has a similar shape to a generic interface. Generic classes have a generic type parameter list in angle brackets (...
Read more >TypeScript Basic Generics - W3Schools
Generics allow creating 'type variables' which can be used to create classes, functions & type aliases that don't need to explicitly define the...
Read more >Typescript Generics Explained - Ross Bulat - Medium
Generics: the ability to abstract types ... The implementation of generics in Typescript give us the ability to pass in a range of...
Read more >TypeScript Generics - TutorialsTeacher
Generics offer a way to create reusable components. Generics provide a way to make components work with any data type and not restrict...
Read more >Understanding TypeScript Generics - Smashing Magazine
Generics have permitted us to specify a type that we wish to permit our list to operate on, and from that, TypeScript can...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This seems to be an issue still.
Intellisense suggestions stop working and code themeing changes too. Attached is a video of what I’m seeing.
after a couple of hours trying to make this work… I realized of a too-simple-to-be-true workaround, just add
as typeof YourGenericComponent
at the endposting it here just in case it helps someone, or myself in the future 😃