1
votes

i have 2 similar questions : I`ve been working with the typescript recently, but i need my styled-component code to be validate to typescript.

1. I need describe custom prop - shadow, because typescript return error

Property 'shadow' does not exist on type 'ThemedStyledProps, HTMLDivElement>, "color" | "style" | "title" | ... 251 more ... | "key"> & { ...; } & { ...; }, 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"};
            `};
        `;

2. how can i describe this props.touched[props.name] in my interface

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;

I used Formik and Yup for my form

1

1 Answers

2
votes

I found an answer for my question =)

for first question

we should create type or interface

type ColorStateForm = {
      errors: any,
      touch: any,
      name: string,
      value: string,
      theme: any,
      hideDefault?: boolean
}

than we use this type or interface in our styled component

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;
      `};
  }

this is where we need to use TypeScript for conditional css on line

 &:before {
    ${props =>
      props.hideDefault &&
      css`

we need to pass our type or interface again. For example:

 &:before {
    ${(props: ColorStateForm ) =>
      props.hideDefault &&
      css`

that`s all, my VS Code is clean and friendly for TypeScript

for second question

First of all I created a type

type ColorStateForm = {
  errors: any,
  touch: any,
  name: string,
  value: string,
  theme: any
}

yes, i know that "any" type is evil, you can write anything instead

than i rewrote my ternary operator like that and added my created type

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;
}

I hope this info will be useful