styled-components: Typescript checking error
import * as React from 'react'
import styled from 'styled-components'
interface ISVGIconProps {
color: string,
children: React.ReactNode,
titleAccess: boolean,
size: string,
viewBox: string
}
const SVG = styled.svg`
display: inline-block;
fill: currentColor;
height: ${(p: any) => p.size};
width: ${(p: any) => p.size};
user-select: none;
flex-shrink: 0;
color: ${(p: any) => p.color};
transition: ${(p: any) => p.theme.transition.create('fill', { duration: p.theme.transition.durations.shorter })};
`
export const SVGIcon: React.StatelessComponent<ISVGIconProps> = (props) => {
const { titleAccess, children, size, ...others } = props
return (
<SVG
width={size}
height={size}
// @ts-ignore
size={size} // <--- this is the issue
focusable="false"
aria-hidden={titleAccess ? 'false' : 'true'}
{...others}
>
{titleAccess ? <title>{titleAccess}</title> : null}
{children}
</SVG>
)
}
SVGIcon.defaultProps = {
color: 'inherit',
size: '1.5em',
viewBox: '0 0 24 24'
}
[ts] Property 'size' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<ThemedOuterStyledProps<SVGProps<SVGSVGEl...'.
Basically any attribute that do not belongs to specific element (whatever that means because after custom element we do not need to do data- anymore but I guess make sense to the old elements?!)
Typescript will complains that any extra attribute I add for the propose of using it in my styled component it will rise this issue.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 5
- Comments: 22 (3 by maintainers)
@DogPawHat no joke
I gave up to Typescript, productivity is more valuable for me than preventing issues that I do not face.
No more Typescript, best scenario
flowfor props validation and I am thinking on usingprop-typesfor that even.The TypeScript fix for this issue has been merged into Master and it can be used with the
nextnpm version. Any example guys from thestyle-componentsteam to fix theIntrinsicAttributes...error?Ok, I looked into this, it’s fixed as far as
styled-componentsare concerned, but there’s some issues with Typescript.Bascilly,
styled-componentsalready has this:https://github.com/Microsoft/TypeScript/issues/11947
where
Ucould be used to define additional prop types:Unfortunaly, this syntax doens’t work, and the issue below has been open for over a year:
https://github.com/Microsoft/TypeScript/issues/11947
So I don’t know when that will get fixed.
That said, usually this works:
at least on TS 2.6.2. your
(p: any)should otherwise be fine, I think.@philpl Issue #630 provides some more context.
styled-componentsdoes not provide any way to extend the prop typings of built-ins that use tagged template literals syntax.TypeScript currently offers no way to pass a generic type interface to a tagged template literal function, so a wrapper function (like the one found in
styled-components-ts) that takes a generic is the only workaround that exists right now.Perhaps this wrapper function could be implemented as part of
styled-componentsso an additional library isn’t needed, but the real solution for this needs to come from Typescript.Hey guys, we were able to get this working via two ways. One way works now but isn’t the official way forward, the other way is the official way forward but is broken until the next TypeScript release (2.9.2)
The interface to allow us to use
mbmargin-bottom shortcut somewhere:Example usage (goal is to have it work w/o TypeScript errors):
The first way that works now:
The future way that will work soon:
You can track the status of fix for future way here (note the related issues at bottom): https://github.com/Microsoft/TypeScript/issues/24449
@viiiprock See https://github.com/jacob-ebey/styled-components-ts
I’m not sure if this is related, but as far as I can tell, there is a bug in the example implementation of styled-components with TypeScript given here https://www.styled-components.com/docs/api#typescript.
I have constructed my file as per the example, however there are two things preventing me to use this as is.
The problematic part of the code is here:
ThemeProvider:If I add the import I need to add a
@ts-ignoreto keep the compiler happy. This is now working when used inside the same codebase. However, I’m using this as a separate component library, which leads to the second issue.theme.d.ts) that I need to use the library in other projects contains the following code snippet:This causes a bunch of errors along the lines of:
I’m not ready to give up on TypeScript yet as it has a bunch of benefits, but it’s definitely killing productivity with issues such as this.
styledComponentWithPropsfrom styled-components-ts just returns what you pass in with types, so its doen’t add much beyond fixing this issue. It might still be the best fix until Microsoft/TypeScript#11947 gets fixed.@yordis have you had any luck with your issues?
@nderscore @alveshelio wait, sorry, why is styled-components-ts a thing? 😅If there are any fixes to our typings, we’d really prefer those to be PR’d and merged here 😉 so is there something we can workout and improve instead of a separate package?
cc @jacob-ebey, @styled-components/typers
Having a similar issue here