[英]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
作为道具, lg
和md
会突出显示,并显示以下错误:
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.