New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typescript error with nested styled, themed components #824
Comments
Is this a bug? And if so, is it a bug with Styled Components, or with Typescript? |
It's not a bug :) you're using the default HTMLProps and trying to pass a ref, but unfortunately the ref is now pointing to an instance of the styled component and not the underlying element ref. You'll either want to add a new type for ref, or more likely use innerRef instead cc @styled-components/typers |
Even more simple example generates compile-time error in TypeScript: const LogoBlock = styled.div`
background-color: lime;`;
const HeaderBlock = styled.div`
border: 1px solid #333;
> ${LogoBlock} {
background: red;
}
`; It gave me following error:
Any ideas how to handle this? |
@sergeyzwezdin the issue you've found has been fixed in v2.0.1 @cesalberca please let us know if you have other issues, I haven't had too much time to play with v2 and typings :) |
Can I ask why this issue has been closed? @patrick91's comment seems to be saying that it's unfixed. |
@gargantuan sorry, I misread his comment :) |
Yep, there's still no fixed. But, I have fixed my issue by creating new React.Component const ButtonDefault = styled(Button)`
background-color: ${ (props) => props.intent ? props.intent : Color.blue } !important;
`
export default class extends React.Component<React.HTMLAttributes<{}> & typeof ButtonDefault.propTypes, {}> {
public render () {
return <ButtonDefault {...this.props}/>
}
} |
@gargantuan this should work: function toInnerRef<T>(ref: undefined | React.Ref<T>): undefined | ((instance: T | null) => void) {
if (!ref) {
return undefined;
}
if (typeof ref === 'string') {
throw new Error('name refs are not supported.');
}
return ref;
}
const MyComponent: React.StatelessComponent<IMyComponentProps> = props => {
const { bar, ref, ...others } = props;
const innerRef = toInnerRef(ref);
return <div><InnerStyled type="text" {...others} innerRef={innerRef} /></div>;
}; Note, that |
Getting a similar issue when using polished 1.8.0 and styled-component 2.2.1:
Results in:
Wrapping
Causes other errors such as:
|
If The fix should be something like this: const Heading = styled.div`
${(props: { width: number; }) => ellipsis(props.width)};
`;
<Header width={12} />; // usage
|
@Igorbek thanks for the reply. I was indeed missing that extraction step for The
Writing my own function with an implicit return type such as:
returns a similar typescript error:
explicitly typing the return as
Typing the return as Again, thanks for your help! |
Ah, I forgot about this bit. Returning an object in interpolation is not allowed at the moment. If you intend to return a style object, then it is something we're going to support very soon, see #1123 |
Great, thanks! |
HI all, Just wondering if i have the same issue related to this one. import * as React from 'react';
import {MouseEventHandler} from 'react';
interface IProps {
className?: string;
onClick?: MouseEventHandler<IProps>;
menuMachineName: string;
}
class Nav extends React.Component<any, IProps> {
public render() {
const {onClick, menuMachineName} = this.props;
// some magic to get the right menu trough a api.
return (menuMachineName);
}
}
export {Nav} And now i want to style the component with styled like this: import {Nav} from './Nav';
const StyledNav = styled(Nav)`
color: red;
`
render() {
return (<div><StyledNav menuMachineName="main" /></div>);
} And then i got this error: Typescript: ^2.6.1 |
class Nav extends React.Component<any, IProps> {
^^^ here should be props |
Hi Igorbek, |
Closing since the original issue was answered by @Igorbek! |
Sorry to reopen an old issue, but still running into issues with this @Igorbek 's solution didn't work for me |
I'm still running into this issue. |
This currently generates the following type error:
|
Version
1.0.5
Reproduction
Not sure how to replicate this in webpackbin
Steps to reproduce
Create a theme file, as per the typescript guidance here
Create two styled components.
{...others} = props
pattern.Expected Behavior
the line
return <div><InnerStyled type="text" {...others}/></div>;
should not generate a typescript error.Actual Behavior
The following typescript error is generated
The text was updated successfully, but these errors were encountered: