Ilj*_*lja 11 javascript jsx typescript reactjs styled-components
我正在尝试将打字稿集成到我们的项目中,到目前为止,我偶然发现了样式组件库的一个问题
考虑这个组件
import * as React from "react";
import styled from "styled-components/native";
import { TouchableOpacity } from "react-native";
// -- types ----------------------------------------------------------------- //
export interface Props {
onPress: any;
src: any;
width: string;
height: string;
}
// -- styling --------------------------------------------------------------- //
const Icon = styled.Image`
width: ${(p: Props) => p.width};
height: ${(p: Props) => p.height};
`;
class TouchableIcon extends React.Component<Props> {
// -- default props ------------------------------------------------------- //
static defaultProps: Partial<Props> = {
src: null,
width: "20px",
height: "20px"
};
// -- render -------------------------------------------------------------- //
render() {
const { onPress, src, width, height } = this.props;
return (
<TouchableOpacity onPress={onPress}>
<Icon source={src} width={width} height={height} />
</TouchableOpacity>
);
}
}
export default TouchableIcon;
Run Code Online (Sandbox Code Playgroud)
以下行抛出3个错误,性质相同 <Icon source={src} width={width} height={height} />
输入{source:any; width:string; height:string;}不能赋值为IntrinsicAttributes ...类型{source:any;}中缺少属性'onPress'; width:string; height:string;}
不完全确定这是什么以及如何解决它,我是否需要声明这些Icon
或类似的东西?
编辑: typescript v2.6.1,样式组件v2.2.3
eln*_*ren 33
有一些最近的发展和新版本的Typescript(例如3.0.1)和样式组件(例如3.4.5),不需要单独的帮助器.您可以直接为样式组件指定道具的界面/类型.
interface Props {
onPress: any;
src: any;
width: string;
height: string;
}
const Icon = styled.Image<Props>`
width: ${p => p.width};
height: ${p => p.height};
`;
Run Code Online (Sandbox Code Playgroud)
如果你想更精确而忽略了 onPress
const Icon = styled.Image<Pick<Props, 'src' | 'width' | 'height'>>`
width: ${p => p.width};
height: ${p => p.height};
`;
Run Code Online (Sandbox Code Playgroud)
Ehs*_*san 19
正如样式组件文档所说,最简单的方法:
import styled from 'styled-components';
import Header from './Header';
const NewHeader = styled(Header)<{ customColor: string }>`
color: ${(props) => props.customColor};
`;
// Header will also receive props.customColor
Run Code Online (Sandbox Code Playgroud)
LOG*_*GAN 13
样式组件
import styled from 'styled-components';
interface Props {
height: number;
}
export const Wrapper = styled.div<Props>`
padding: 5%;
height: ${(props) => props.height}%;
`;
Run Code Online (Sandbox Code Playgroud)
指数
import React, { FunctionComponent } from 'react';
import { Wrapper } from './Wrapper';
interface Props {
className?: string;
title: string;
height: number;
}
export const MainBoardList: FunctionComponent<Props> = ({ className, title, height }) => (
<Wrapper height={height} className={className}>
{title}
</Wrapper>
);
Run Code Online (Sandbox Code Playgroud)
应该管用
您只需指定一个interface
:
import { createGlobalStyle, css } from 'styled-components';
interface PropsGlobalStyle {
dark: boolean
}
export default createGlobalStyle`
${({ dark }: PropsGlobalStyled) => css`
body {
box-sizing: border-box;
margin: 0;
font-family: Arial, Helvetica, sans-serif;
color: ${dark ? '#fff' : '#000'};
background-color: ${dark ? '#000' : '#fff'};
}
`};
`;
Run Code Online (Sandbox Code Playgroud)
小智 6
ColorCard
使用color 属性的示例
import styled from 'styled-components';
export const ColorCard = styled.div<{ color: string }>`
background-color: ${({ color }) => color};
`;
Run Code Online (Sandbox Code Playgroud)
这个答案已经过时,最新的答案是在这里:https : //stackoverflow.com/a/52045733/1053772
据我所知,尚无官方方法(但是?),但是您可以通过一些技巧来解决。首先,创建一个withProps.ts
具有以下内容的文件:
import * as React from 'react'
import { ThemedStyledFunction } from 'styled-components'
const withProps = <U>() => <P, T, O>(fn: ThemedStyledFunction<P, T, O>) =>
fn as ThemedStyledFunction<P & U, T, O & U>
export { withProps }
Run Code Online (Sandbox Code Playgroud)
现在,在您的.tsx
文件内部,像这样使用它:
// ... your other imports
import { withProps } from './withProps'
export interface IconProps {
onPress: any;
src: any;
width: string;
height: string;
}
const Icon = withProps<IconProps>()(styled.Image)`
width: ${(p: IconProps) => p.width};
height: ${(p: IconProps) => p.height};
`;
Run Code Online (Sandbox Code Playgroud)
而且您应该很好。这绝对不是理想的,希望很快就会有一种在TS中为模板文字提供泛型的方法,但是我想现在这是您最好的选择。
信用归功于应得的信用:我从这里复制粘贴
归档时间: |
|
查看次数: |
8889 次 |
最近记录: |