gpt4 book ai didi

javascript - 使用 typescript jsx 的 ESLint 配置

转载 作者:行者123 更新时间:2023-12-04 17:12:07 24 4
gpt4 key购买 nike

我在使用 typescript 进行配置时遇到问题。这是我在 tsconfig.json 中的以下代码:

{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve"
},
"include": [
"src"
]
}

这是我遇到的错误:

Failed to compile.

./src/Components/AdvancedSearch/AdvancedSearch.tsx Line 80:21:'JSX' is not defined no-undef

Search for the keywords to learn more about each error.

导致错误 AdvancedSearch.tsx 的文件:

在 AdvancedSearch 中编辑和更新完整代码

type AdvancedSearchState = {
containerHeight: number,
showMore: boolean,
transitioning: boolean;
};
type Props = {
show: boolean;
// selected: [ContractType];
selected: any;
onChange: (e: any) => void;
contracts: ContractType[];
};
class AdvancedSearch extends React.Component<Props, AdvancedSearchState> {
advancedSearchContainer: React.RefObject<HTMLDivElement>;
advancedSearchWrapper: React.RefObject<HTMLDivElement>;
width: number = 3;
labelStyle = {
color: "#1e7e34",
"text-decoration": "underline"
};
constructor(props: Props) {
super(props);
this.selectItem = this.selectItem.bind(this);
this.advancedSearchContainer = React.createRef();
this.advancedSearchWrapper = React.createRef();
this.resize = this.resize.bind(this);
this.state = {
showMore: false,
containerHeight: 0,
transitioning: true
};
}
getContainerHeight() {
let containerHeight = 0;
if (this.advancedSearchContainer.current) {
containerHeight = this.advancedSearchContainer.current.clientHeight;
}
return containerHeight;
}
resize() {
let containerHeight = this.getContainerHeight();
if (this.state.containerHeight !== containerHeight) {
this.setState({ containerHeight: containerHeight });
}
}
componentDidMount() {
this.setState({ containerHeight: this.getContainerHeight() });
window.addEventListener("resize", this.resize);
}
componentWillUnmount() {
window.removeEventListener("resize", this.resize);
}
componentDidUpdate() {
console.log(this.state.containerHeight);
this.resize();
}
selectItem(name: string) {
// let selectedContract = name.currentTarget.name;
// currently change the selectedContract as just the string
let selectedContract = name;
let selected = this.props.selected;
let inx = this.props.selected.indexOf(selectedContract);
if (inx > -1) {
selected.splice(inx, 1);
} else {
selected.push(selectedContract);
}
let event = {
target: {
value: selected,
name: "contracts"
}
};
this.props.onChange(event);
}
chunkArray(array: JSX.Element[], width: number) {
return array.reduce((acc: any[][], item: any, index: number) => {
let loc = Math.floor(index / width);
if (!acc[loc]) {
acc[loc] = [];
}
acc[loc].push(item);
return acc;
}, []);
}

render() {
//TODO: Should be passed in and not the list of contracts
let initialList = this.chunkArray(
this.props.contracts.map(contractType => {
return (
<div className="four columns contract-container">
<span className="contract-header">
{contractType.contractTypeName}
</span>
<dl className="contract-list">
{contractType.contracts.map(contract => {
return (
<li className="contract">
<MvwCheckbox
labelStyle={this.labelStyle}
onChange={this.selectItem}
checked={this.props.selected.indexOf(contract.name) >= 0}
label={contract.name}
name={contract.name}
/>
</li>
);
})}
</dl>
</div>
);
}),
this.width
);
let list;
if (this.state.showMore) {
list = initialList.map((item: React.ReactNode) => {
return <div className="row">{item}</div>;
});
} else {
list = [initialList[0]].map(item => {
return <div className="row">{item}</div>;
});
}
return (
<div
className={
"twelve column advanced-search " + (this.props.show ? "show" : "")
}
>
<div
className="advanced-search-wrapper"
ref={this.advancedSearchWrapper}
style={{ height: this.props.show ? this.state.containerHeight : 0 }}
>
<div
className="advanced-search-content"
ref={this.advancedSearchContainer}
>
<div className="advanced-search-body">
<div className="advanced-search-title">
<p>
Please select the product(s) you wish to use for your
Reservation Search:
</p>
</div>
<div className="advanced-search-list">{list}</div>
</div>
</div>
</div>
</div>
);
}
}
export default AdvancedSearch;

更新并添加了 AdvancedSearch 文件的导入:

import React from "react";
import MvwCheckbox from "../../Generic/MvCheckBox";
import "./AdvancedSearch.css";
import ContractType from "../../Interfaces/AdvanceSearchInterface"

最佳答案

no-undef 在此实例中导致 ESLint/TypeScript 兼容性问题。查看FAQ其中特别提到了您的问题。我在这里只引用相关部分:

We strongly recommend that you do not use the no-undef lint rule on TypeScript projects. The checks it provides are already provided by TypeScript without the need for configuration - TypeScript just does this significantly better.

As of our v4.0.0 release, this also applies to types. If you use global types from a 3rd party package (i.e. anything from an @types package), then you will have to configure ESLint appropriately to define these global types. For example; the JSX namespace from @types/react is a global 3rd party type that you must define in your ESLint config.

查看此 ESLint定义全局变量的帮助指南。您需要将 globals 部分添加到您的 .eslintrc 中,其中包括 JSX:

"globals": {
"JSX": "readonly",
},

您可以通过在 .eslintrc 中定义 rules 为项目完全关闭 no-undef:

"rules": {
"no-undef": "off"
}

或者你可以添加一个 overrides 部分来为 typescript 文件关闭这个规则,特别是如果你有一个混合的 TS/JS 项目:

"overrides": [
{
"files": ["*.ts", "*.tsx"],
"rules": {
"no-undef": "off"
}
}
]

关于javascript - 使用 typescript jsx 的 ESLint 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69231333/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com