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.

TypeScript props and HTML attributes collisions

See original GitHub issue
  • emotion version: 10.0.0
  • react version: ^16

Relevant code:

type Props = {
  color: string | string [];
}

const MyComponent = styled('div')<Props>`
  color: pink;
`;

What you did:

I want a prop that coincides with the name of an attribute on the HTMLElement type (i.e.: color exists on a div element as a type string, but I want a prop called color which could be either a string or a string[]). For a little bit of more context, this is how styled-system works with its exported style prop color and type ColorProps.

What happened:

image

The relevant debugging information is Type 'string | string[]' is not assignable to type 'string'.

Reproduction:

Simply copy the code snippet above into your editor.

Problem description:

I’m rendered unable to type this component without renaming that prop, which I really don’t want to do because that breaks the styled-system contract.

Suggested solution:

No ideas! Any help welcome.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

15reactions
immasandwichcommented, Mar 14, 2019

Our workaround is:

const MyComponent = styled<'div', Props>('div')`
  color: pink;
`;

…which allows the ExtraProps to override the attributes on a div element.

3reactions
pottycommented, Aug 1, 2019

For me, the solution was to use Radix https://github.com/modulz/radix instead of styled-system directly. Radix uses some parts of styled-system and has better TS support.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A typescript type that allows any html attribute - Stack Overflow
I', trying to create a react component which emulates a native html element. Problem 1:.
Read more >
Web Components API: Definition, Attributes And Props
One thing to be careful about is defining your own attributes for the web component. They don't have to collide with any native...
Read more >
TypeScript + React = ❤️ by Ben Ilegbodu - GitNation
TypeScript is a JavaScript superset that compiles down to vanilla JavaScript and has become increasingly popular.
Read more >
Polymorphic React Components in TypeScript | Ben Ilegbodu
Basically we want to merge the properties of Props and the properties of C together. But if there are prop name collisions, TypeScript...
Read more >
Documentation - Object Types - TypeScript
Optional Properties. Much of the time, we'll find ourselves dealing with objects that might have a property set. In those cases, we 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