question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generics support for typescript

See original GitHub issue

When 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:closed
  • Created 5 years ago
  • Reactions:77
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

31reactions
DevanBcommented, Oct 17, 2018

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.

ezgif com-crop

27reactions
jperellicommented, Mar 12, 2020

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 end

// antd -> Table is a generic component
import styled from 'styled-components'
import { Table as AntdTable } from 'antd'

export const Table = styled(AntdTable)`
...
` as typeof AntdTable

posting it here just in case it helps someone, or myself in the future 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found