【问题标题】:Emotion Js and TypeScript problems when passing props to styled将道具传递给样式时的情感 Js 和 TypeScript 问题
【发布时间】:2020-08-08 12:14:44
【问题描述】:

我收到了这个错误:

No overload matches this call.   Overload 1 of 2, '(...styles: Interpolation<Pick<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "form" | ... 264 more ... | "value"> & { ...; }>[]): StyledComponent<...>', gave the following error.
    Argument of type 'TemplateStringsArray' is not assignable to parameter of type 'Interpolation<Pick<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "form" | ... 264 more ... | "value"> & { ...; }>'.
      Type 'TemplateStringsArray' is not assignable to type 'ObjectInterpolation<Pick<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "form" | ... 264 more ... | "value"> & { ...; }>'.
        Types of property 'filter' are incompatible.
          Type '{ <S extends string>(callbackfn: (value: string, index: number, array: readonly string[]) => value is S, thisArg?: any): S[]; (callbackfn: (value: string, index: number, array: readonly string[]) => unknown, thisArg?: any): string[]; }' is not assignable to type 'string | string[] | undefined'.
            Type '{ <S extends string>(callbackfn: (value: string, index: number, array: readonly string[]) => value is S, thisArg?: any): S[]; (callbackfn: (value: string, index: number, array: readonly string[]) => unknown, thisArg?: any): string[]; }' is missing the following properties from type 'string[]': pop, push, concat, join, and 27 more.   Overload 2 of 2, '(template: TemplateStringsArray, ...styles: Interpolation<Pick<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "form" | ... 264 more ... | "value"> & { ...; }>[]): StyledComponent<...>', gave the following error.
    Argument of type '(props: HamburguerProps) => "250px" | "0"' is not assignable to parameter of type 'Interpolation<Pick<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "form" | ... 264 more ... | "value"> & { ...; }>'.
      Type '(props: HamburguerProps) => "250px" | "0"' is not assignable to type 'FunctionInterpolation<Pick<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "form" | ... 264 more ... | "value"> & { ...; }>'.
        Types of parameters 'props' and 'mergedProps' are incompatible.
          Property 'open' is missing in type 'Pick<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "form" | ... 264 more ... | "value"> & { ...; }' but required in type 'HamburguerProps'.  TS2769

关于此代码:

> 25 | export const Hamburguer = styled('button')`
       |                           ^
    26 |   position: absolute;
    27 |   left: ${(props: HamburguerProps) => (props.open ? '250px' : '0')};
    28 | `;

我以为问题与我的代码有关,但做了所有测试,我相信不是那个

我查找了这个错误或解决方案,但没有找到

我的TsConfig

{
  "compilerOptions": {
    "target": "es5",
    "types": ["node", "@emotion/core"],
    "lib": ["dom", "dom.iterable", "esnext"],
    "baseUrl": "src",
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": ["src"]
}

我的 Tsx 代码:

import Logo from 'assets/images/Logo.png';
import { useSelector, useDispatch } from 'react-redux';
import { toggleSide } from 'redux/slices/sideBar';

interface RootState {
  sideBarStatus: boolean;
}

interface SideBar {
  isOpen: boolean;
}

const LogoNavigation: React.FC<SideBar> = ({ isOpen }) => {
  const dispatch = useDispatch();
  return (
    <LogoSide>
      <img src={Logo} alt="Logo Elo Ghost" />
      <Hamburguer open={isOpen} onClick={() => dispatch(toggleSide(!isOpen))}>
        <div />
        <div />
        <div />
      </Hamburguer>
    </LogoSide>
  );
};

const SideNavigation: React.FC = () => {
  // const { sideIsOpen } = useSelector((RootState) => RootState.toggleSide);
  const selectIsOpen = (state: RootState) => state.sideBarStatus;
  const sideBarStatus = useSelector(selectIsOpen);
  return (
    <SideNav>
      <LogoNavigation isOpen={sideBarStatus} />
    </SideNav>
  );
};

export default SideNavigation;

我的情感风格

import styled from '@emotion/styled';

type HamburguerProps = {
  open: boolean;
};

export const SideNav = styled('nav')`
  max-width: 250px;
  width: 100%;
  height: 100vh;
  background: #437fb9;
`;

export const LogoSide = styled('div')`
  display: flex;
  justify-content: space-between;
  width: 100%;
  height: 60px;
  background: #fdca40;
  img {
    height: 50px;
  }
`;

export const Hamburguer = styled('button')`
  position: absolute;
  left: ${(props: HamburguerProps) => (props.open ? '250px' : '0')};
`;

在这里我有一个 gif 用于渲染我的组件,但在错误出现之后

【问题讨论】:

    标签: reactjs typescript emotion


    【解决方案1】:

    如果您尝试extend the styles 现有组件(使用styled-components@emotion/styled),语法会略有不同。例如,如果我们有一个正在尝试扩展的BaseButton

    interface BaseButtonProps {
      color: string
    }
    
    const BaseButton = styled.button<BaseButtonProps>`
      ${props => {
        if (props.color === 'red') {
          return `
            color: red;
          `;
        }
    
        return `
          color: green;
        `;
      }}
    `;
    
    ...
    
    interface HamburguerProps extends BaseButtonProps {
      open: boolean
    }
    
    export const Hamburguer = styled(BaseButton)<HamburguerProps>`
      position: absolute;
      left: ${(props) => (props.open ? '250px' : '0')};
    `;
    

    确保将HamburguerProps 接口参数放在右侧

    styled(BaseButton)<HamburguerProps>
    

    而不是左侧

    styled<HamburguerProps>(BaseButton)
    

    【讨论】:

      【解决方案2】:

      当您使用 JSS 库(例如 Emotion 和 Styled Components)和 TypeScript 时,要使 props 工作,您必须提供指定类型的泛型。这可以通过使用接口或类型别名来完成。

      对于您的情况,您需要通过使用 styled&lt;HamburguerProps&gt; 来具体说明 Hamburger 具有 HamburguerProps

      export const Hamburguer = styled.button<HamburguerProps>`
        position: absolute;
        left: ${(props) => (props.open ? '250px' : '0')};
      `;
      

      【讨论】:

      • hiii 我收到了这个错误:类型“HamburguerProps”不满足约束“符号”| “对象” | “导航” | “一个” | “缩写” | “地址” | “面积” | “文章” | “一边” | “音频” | "b" | “基地” | "bdi" | "bdo" | “大” | "块引用" | “身体” | "br" | “按钮” | “画布” | ... 154 更多... | “看法”'。类型“HamburguerProps”不可分配给类型“视图”
      • styled.button`这项工作非常感谢你的兄弟。
      • 可以帮帮我吗兄弟? stackoverflow.com/questions/61427095/…
      • 由于某种原因,根据我的状态工作的动画无法正常工作
      猜你喜欢
      • 2019-03-08
      • 2017-10-20
      • 2021-03-30
      • 2020-04-03
      • 2023-03-24
      • 2020-08-08
      • 1970-01-01
      • 2021-05-28
      • 2021-06-29
      相关资源
      最近更新 更多