简体   繁体   English

Typescript 错误:X 类型的参数不能分配给 Y 类型的参数

[英]Typescript error: Argument of Type X is not assignable to parameter of type Y

New to Typescript, and trying to understand how I can fix the below errors. Typescript 的新手,并试图了解如何修复以下错误。 If I remove the ItemProps as props, lg and md are highlighted and the following error shows:如果我删除ItemProps作为道具, lgmd会突出显示,并显示以下错误:

Type 'string' is not assignable to type 'ColSpec | undefined'.ts(2322)

So then I try to extend the ColProps, and then I get所以然后我尝试扩展 ColProps,然后我得到

Argument of type '(item: Item) => JSX.Element' is not assignable to parameter of type '(value: { title: string; content: string; social: { title: string; link: string; }[]; lg: string; links?: undefined; md?: undefined; contentBottomMargin?: undefined; form?: undefined; } | { title: string; links: { ...; }[]; ... 5 more ...; form?: undefined; } | { ...; }, index: number, array: ({ ...; } | ... 1 more ...'.
  Types of parameters 'item' and 'value' are incompatible.
    Type '{ title: string; content: string; social: { title: string; link: string; }[]; lg: string; links?: undefined; md?: undefined; contentBottomMargin?: undefined; form?: undefined; } | { title: string; links: { ...; }[]; ... 5 more ...; form?: undefined; } | { ...; }' is not assignable to type 'Item'.
      Type '{ title: string; content: string; social: { title: string; link: string; }[]; lg: string; links?: undefined; md?: undefined; contentBottomMargin?: undefined; form?: undefined; }' is not assignable to type 'Item'.
        Property 'md' is optional in type '{ title: string; content: string; social: { title: string; link: string; }[]; lg: string; links?: undefined; md?: undefined; contentBottomMargin?: undefined; form?: undefined; }' but required in type 'Item'.`

See code below:请参见下面的代码:

Footer.tsx file Footer.tsx文件

import {
  Container,
  Row,
  Col,
  ColProps
} from "react-bootstrap";

import footerContent from "./footer.json";

interface Item extends ColProps {
  lg: string | ColProps["lg"];
  md: string | ColProps["md"];
}

const Footer = () => {
  return (
    <Container>
      <Row>
        {footerContent.map((item) => (
          <Col
            key={item.title}
            lg={item.lg && item.lg}
            md={item.md && item.md}
            className="mb-5 mb-lg-0"
          >
            <div className="fw-bold text-uppercase text-dark mb-3">
              {item.title}
            </div>
          </Col>
        ))}
      </Row>
    </Container>
  );
};

export default Footer;

then footer.json file然后footer.json文件

[
  {
    "title": "App",
    "lg": "4"
  },
  {
    "title": "Stuff",
    "lg": "2",
    "md": "6"
  }
]

I've taken a look at Overriding interface property type defined in Typescript d.ts file , as well as some other answers, and have tried overriding, extending, omitting, etc. to no avail.我查看了 Typescript d.ts 文件中定义的覆盖接口属性类型以及其他一些答案,并尝试了覆盖、扩展、省略等无济于事。 What could I do here to get the errors to stop, and the code to work?我可以在这里做什么来停止错误并使代码正常工作?

I think you are almost there, you just needed to assign the right type to the imported JSON (or you can assign the right type to item in your .map callback.我想你快到了,你只需要为导入的 JSON 分配正确的类型(或者你可以在.map回调中为item分配正确的类型。

import React from 'react';
import {
  Container,
  Row,
  Col,
  ColProps
} from "react-bootstrap";

// if you are importing JSON, cast it
// import _footerContent from "./footer.json"; 
// const footerContent = _footerContent as FooterContent[];

type FooterContent = {
  title: string;
  lg: ColProps['lg'];
  md?: ColProps['md'];
}
const footerContent: FooterContent[] = [
  {
    "title": "App",
    "lg": "4"
  },
  {
    "title": "Stuff",
    "lg": "2",
    "md": "6"
  }
];

const Footer = () => {
  return (
    <Container>
      <Row>
        {footerContent.map((item) => (
          <Col
            key={item.title}
            lg={item.lg && item.lg}
            md={item.md && item.md}
            className="mb-5 mb-lg-0"
          >
            <div className="fw-bold text-uppercase text-dark mb-3">
              {item.title}
            </div>
          </Col>
        ))}
      </Row>
    </Container>
  );
};

export default Footer;

TS Playground: https://tsplay.dev/wOzlzW TS游乐场: https://tsplay.dev/wOzlzW

I also think,我也觉得,

            lg={item.lg && item.lg}
            md={item.md && item.md}

is just verbose without purpose and same as只是冗长而没有目的,与

            lg={item.lg}
            md={item.md}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿:X 类型的参数不能分配给 Y 和 X 类型的参数。X 类型不能分配给 Y 类型 - Typescript: Argument of type X is not assignable to parameter of type Y & X. Type X is not assignable to type Y Typescript 错误“类型参数不可分配给类型参数” - Typescript error 'argument of type is not assignable to parameter of type' &#39;x&#39;类型的React Typescript参数不可分配给&#39;SetStateAction&#39;类型的参数<x]> &#39; - React Typescript Argument of type 'x' is not assignable to parameter of type 'SetStateAction<x]>' React Typescript:'{ [x: number]: any; 类型的参数 }' 不可分配给类型的参数 - React Typescript: Argument of type '{ [x: number]: any; }' is not assignable to parameter of type 设置组件状态时出现 TypeScript 错误:“X”类型的参数不可分配给“() =&gt; void”类型的参数 - TypeScript error when setting a component's state: Argument of type 'X' is not assignable to parameter of type '() => void' CombineReducers与Typescript返回错误“类型的参数不能分配给&#39;ReducersMapObject&#39;类型的参数” - CombineReducers with Typescript returns error “Argument of type is not assignable to parameter of type 'ReducersMapObject'” 打字稿错误:参数类型无法分配给参数类型,但它们是相同的 - Typescript error: Argument type is not assignable to parameter type, but they are the same useState Typescript 错误:“prev”类型的参数不可分配给类型参数 - useState Typescript Error: Argument of type 'prev' is not assignable to parameter of type React 组件上的 Typescript 错误:“元素”类型的参数不可分配给类型的参数 - Typescript error on React Component: Argument of type 'Element' is not assignable to parameter of type Typescript,useContext(),错误:“never[]”类型的参数不可分配给类型参数 - Typescript, useContext(), error: Argument of type 'never[]' is not assignable to parameter of type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM