【发布时间】:2021-07-10 09:18:20
【问题描述】:
感谢您花时间阅读本文。
我正在尝试使用带有 AirBnB 配置的 eslint 来学习 Typescript。像映射一组对象并为每个对象创建一个自定义功能组件这样简单的事情给了我下一个错误:
Argument of type '(product: ProductType) => JSX.Element' is not assignable to parameter of type '(value: object, index: number, array: object[]) => Element'.
这里是父组件ProductGrid:
const ProductGrid: React.FC = () => {
const products = [
{
id: "string1",
name: "string1",
price: {
formatted_with_symbol: "string1"
},
description: "string1",
},
...
];
return (
<div>
<Grid container spacing={3}>
<Grid item xs={12} md={6} lg={4}>
{products.map((product: ProductType) => (
<Product key={product.id} product={product} />
))}
</Grid>
</Grid>
</div>
);
};
export default ProductGrid;
这是子组件Product:
const Product: React.FC<ProductTypeAsProps> = ({product}: ProductTypeAsProps) => {
const [loading, setLoading] = useState(true);
setTimeout(() => {
setLoading(false);
}, 3000);
return (
<Box className="product">
<Card className="card">
<CardContent className="content">
{loading ? (
<div className="skeleton">
<Skeleton
variant="text"
animation="wave"
component="h2"
width="65%"
/>
</div>
) : (
<div>
<p>{product.name}</p>
<p>{product.description}</p>
<p>{product.price.formatted_with_symbol}</p>
</div>
)}
</CardContent>
</Card>
</Box>
);
};
export default Product;
以及类型声明:
export type ProductType = {
id: string;
name: string;
price: {
formatted_with_symbol: string;
};
description: string;
assets: [
{
id: string;
filename: string;
url: string;
}
];
};
export interface ProductTypeAsProps {
product: ProductType;
}
我也有一个 .eslintrc :
{
"extends": ["airbnb-typescript", "react-app", "prettier"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"sourceType": "module",
"project": "tsconfig.json"
},
"plugins": ["prettier", "@typescript-eslint", "react-hooks"],
"settings": {
"import/resolver": {
"typescript": {
"alwaysTryTypes": true
}
}
},
"rules": {
"@typescript-eslint/no-explicit-any": [
"error",
{
"ignoreRestArgs": true
}
],
}
}
和 tsconfig :
{
"compilerOptions": {
"target": "es5",
"lib": ["es6", "dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"noImplicitAny": false
},
"include": ["src"]
}
我已经创建了this codesandbox,但当然,lint 错误并没有出现在那里,我在 VsCode 中对此进行了编码。
有什么帮助吗?因为这在 javascript 中是相当标准的。
【问题讨论】:
-
我应该提到这是从 commerce.js 中提取的,这意味着我从后端收到的
product对象要大得多,但到目前为止我只使用类型化的属性。我相信这没问题。
标签: reactjs typescript eslint eslint-config-airbnb