Background:
I'm doing my first steps with Typescript-React-styledComponents,
and I'm trying this simple case where I'm passing a size prop via a parent component, which will set the font-size of the child component.
EDITED:
Error:
ts throws me this:
Property 'size' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<ThemedOuterStyledProps<DetailedHTMLProps...'.
Questions:
I noticed
sizemight be a problematic prop name, cuz I don't get any error on the other props. do you know why?more general question - what am I missing?
Code:
Parent Component
<Parent>
<Child text="Red Bikini" color='red' size={2}/>
</Parent>
Child Component
import * as React from 'react';
import styled from 'styled-components';
//EDITED
interface headerSizeType {
1: string;
2: string;
3: string;
4: string;
5: string;
6: string;
}
const size: headerSizeType = {
1: '2em',
2: '1.5em',
3: '1.17em',
4: '1em',
5: '.83em',
6: '.67em'
}
interface ChildProps {
text: string;
size?: number;
color?: string;
style?: any;
children?: React.ReactChildren;
className?: string;
}
const ChildStyle = styled.h2`
display: inline-block;
color: ${(props: any) => props.color || 'black'};
font-size: ${props => size[props.size] || 'black'}; //1st Error here
`;
const Child= (props: ChildProps): JSX.Element => {
console.log(props.color)
return (
<ChildStyle color={props.color} size={props.size}> //2nd Error here
{ props.text }
</ChildStyle>
);
};
export default Title;
const size: any = {...}... what would possess you to write something so terrible? - Aluan Haddadconst size = {...}and the language will infer a rich, well checked type for you. Usinganythere completely defeats the purpose of the language. - Aluan Haddad