- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 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; theJSX
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/
我遵循文档中的说明 https://www.gatsbyjs.org/docs/eslint/ ,并想覆盖其中一条规则,但不影响其他规则,我所做的是创建一个 .eslintrc.js 文件。 这是文件
是否可以覆盖 TSLINT (tslint.json) 中特定文件的规则,就像沿着这些行的配置一样: "overrides": [{ "files": [ "*.spec.js" ], "ru
我一直试图让 ESLint 在 VS Code 中处理一个新的 Angular 项目,但它无法加载“@typescript-eslint/eslint-plugin”。在过去的 3 个小时里,我一直在
我在某个文件夹中有一些文件,而这些文件来自供应商,因此我无法控制它们。我想为该目录和所有子文件夹中的所有文件禁用 eslint。 最佳答案 您可以添加 .eslintignore在项目根目录下,和.g
我第一次尝试使用汇总,但我不明白为什么会出现此错误: TypeError: eslint is not a function 我之前安装了 rollup (v1.1.0) 然后 eslint npm
我正在尝试使用预提交 Hook 在提交发生之前检测 eslint 错误。我正在使用 husky 和 lint-staged。但它对 src 中的所有文件运行 lint 命令,而不是仅对暂存文件运行
我不确定我正在使用的东西是否存在错误,或者我是否只是在这里设置了错误,但是我在运行 eslint src --fix 关于“eslint-plugin-@typescript-eslint” 我已经指
我正在按照说明从 here 设置 ESLint . ESLint 失败并显示以下消息: $ npm run lint > IACS@1.0.0 lint /some/path/sscce-typesc
我正在使用 airbnb-eslint 和 babel-plugin-module-resolver。我在使用别名导入的每个 js 文件中都收到此错误。 { "extends": ["plugi
我正在将我的项目从 tslint 重新配置为 eslint。我可以手动运行 eslint,但 webpack 无法运行并显示以下错误消息: Module build failed (from /pat
我正在尝试使用 svelte 制作一个应用程序来试用它。 我想设置 prettier 和 eslint,我安装了这些依赖项和 Svelte for VS Code 的扩展。 "dependenci
我正在寻找一个规则来对齐所有 =在任务上。 像这样的事情将是积极的: var foo = 12; var barfoo = 21; var barfoobar = 22; 像这样的事
设置后eslint并在忽略列表中添加一些文件,每次运行 eslint 时,它都会生成有关被忽略文件的警告: /path/to/file/name.min.js 0:0 warning Fil
是否可以将 ESLint 配置为仅将规则应用于名称与特定模式匹配的文件?我知道在目录中可以有单独的规则表,但如果结构如下: app | |- module1 | |- module1.js |
有没有办法禁用文件夹的特定规则?例如,我不希望 test 文件夹中的所有测试文件都包含必需的 JSDoc 注释。有办法做到这一点吗? 最佳答案 要从 eslint 规则中忽略某些文件夹,我们可以在根目
缩进规则似乎无法禁用;如何(在配置设置中)禁用此规则?谢谢。 最佳答案 将规则设置为 "off"在你的配置中是这样的: "rules": { "indent": "off" } 您可以在 the
ESLint 有 dot-location允许决定您是否愿意的规则 object. property 或者 object .property 当打破点时(我更喜欢后者)。 但是,我找不到确保点与对象具
这些库之间有什么区别? babel-eslint [ github.com/babel/babel-eslint ] eslint-plugin-babel [github.com/babel/esl
我想把 Prettier 和 ESLint 一起用,但是一个接一个用就遇到了一些冲突。我看到有这三个包似乎允许它们串联使用: prettier-eslint eslint-plugin-prettie
我正在尝试使用React.js和TypeScrip在ASP.NET核心MVC项目中启用eslint linting,并且正在努力修复上面的错误。。我使用的是Visual Studio2022社区版17
我是一名优秀的程序员,十分优秀!