Ром*_*рич 4 typescript reactjs styled-components yup formik
我有 2 个类似的问题:我最近一直在使用打字稿,但我需要将我的样式组件代码验证为打字稿。
1. 我需要描述自定义道具 - 阴影,因为打字稿返回错误
属性 'shadow' 不存在于类型 'ThemedStyledProps, HTMLDivElement>, "color" | "风格" | “标题” | ... 251 更多... | “键”> & { ...; } & { ...; }, DefaultTheme>'。TS2339
export const InputBlock = styled.div<{height: string}>`
display: flex;
width: 100%;
height: ${props => (props.height ? props.height : "56px")};
${props =>
props.shadow &&
css`
box-shadow: ${props =>
props.shadow ? "4px 4px 10px rgba(31,31,31,0.1)" : "none"};
`};
`;
Run Code Online (Sandbox Code Playgroud)
2. 我如何在我的界面中描述这个 props.touched[props.name]
interface FormSchema {
errors: {
[name: string]?: string, // ????
},
touched: {
[propName: string]?: string, // ???
},
value: string,
theme: {
color: {
redError?:string,
inactiveBlue?:string,
green?:string,
}
}
}
const colorStateForm = (props: FormSchema) =>
props.errors &&
props.touched &&
props.errors[props.name] &&
props.touched[props.name]
? props.theme.color.redError
: !props.value && (!props.value.length || props.value.length === 0)
? props.theme.color.inactiveBlue
: props.theme.color.green;
Run Code Online (Sandbox Code Playgroud)
我用 Formik 和 Yup 作为我的表格
我找到了我的问题的答案 =)
第一个问题
我们应该创建类型或接口
type ColorStateForm = {
errors: any,
touch: any,
name: string,
value: string,
theme: any,
hideDefault?: boolean
}
Run Code Online (Sandbox Code Playgroud)
比我们在样式化组件中使用这种类型或接口
export const CheckBoxCustom = styled.div<ColorStateForm >`
width: ${props => (props.width ? props.width : "24px")};
height: ${props => (props.height ? props.height : "24px")};
margin: ${props => (props.margin ? props.margin : "0")};
position: relative;
overflow: hidden;
border-radius: 4px;
flex: 0 0 24px;
&:before {
${props =>
props.hideDefault &&
css`
content: "";
position: absolute;
top: 0;
left: 0;
display: block;
width: ${props => (props.width ? props.width : "24px")};
height: ${props => (props.height ? props.height : "24px")};
border-style: solid;
border-width: 2px;
border-color: ${props =>
props.errors &&
props.touched &&
props.errors[props.name] &&
props.touched[props.name]
? props.theme.color.redError
: props.theme.color.mainBlue};
box-sizing: border-box;
`};
}
Run Code Online (Sandbox Code Playgroud)
这是我们需要在线使用 TypeScript 进行条件 css 的地方
&:before {
${props =>
props.hideDefault &&
css`
Run Code Online (Sandbox Code Playgroud)
我们需要再次传递我们的类型或接口。例如:
&:before {
${(props: ColorStateForm ) =>
props.hideDefault &&
css`
Run Code Online (Sandbox Code Playgroud)
就是这样,我的 VS Code 对 TypeScript 干净且友好
对于第二个问题
首先我创建了一个类型
type ColorStateForm = {
errors: any,
touch: any,
name: string,
value: string,
theme: any
}
Run Code Online (Sandbox Code Playgroud)
是的,我知道“任何”类型都是邪恶的,你可以写任何东西来代替
比我这样重写我的三元运算符并添加我创建的类型
const colorStateForm = (props: ColorStateForm): string => {
const {errors, touched, name, value, theme} = props;
if(errors && touched && errors[name] && touched[name]){
return theme.coloor.redError;
}
if(!value && (!value.length || value.length === 0)) {
return theme.color.inactiveBlue;
}
return theme.color.green;
}
Run Code Online (Sandbox Code Playgroud)
我希望这些信息会有用
| 归档时间: |
|
| 查看次数: |
1307 次 |
| 最近记录: |