【发布时间】:2021-12-29 07:29:20
【问题描述】:
在我们的 CRA 项目中,我们有这样的代码:
const patTable = tables['pat'].at(0);
Array.prototype.at() 功能是一个新功能,在旧版浏览器(例如 Chrome 89)中不可用。但由于某种原因,CRA 甚至没有发出警告或错误消息,并且很容易接受它。它还没有填充 .at() 函数,现在它正在破坏旧浏览器中的应用程序。
WebStorm 显示的推断类型为Table | undefined(由于@types/node 16.x,但我不确定)。
还要注意 TypeScript 还不支持.at(),例如:
function foo(values: string[]) {
const value = values.at(0);
}
引发错误:Property 'at' does not exist on type 'string[]'.(2339)
您是否有任何想法,TS 检查、linting 或构建都没有检测到问题怎么可能?
TS版本为:4.3.5
tsconfig.json 是:
{
"compilerOptions": {
"target": "es6",
"lib": ["dom", "dom.iterable", "esnext"],
"sourceMap": true,
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"jsx": "react-jsx",
"baseUrl": "src",
"noFallthroughCasesInSwitch": true
},
"include": ["src", "types/global-fetch.d.ts"]
}
【问题讨论】:
-
"lib": ["dom", "dom.iterable", "esnext"],注意esnext -
不,它不影响它。在我们的项目中,唯一受
esnext影响的行是someString.replaceAll(...)。 100%。 -
@Onkeltem - 虽然它现在不会,但它会,所以你需要考虑
"esnext"是否真的适合这个项目。 -
谢谢@T.J.Crowder,我明白了。是的,我宁愿附上一些特定的 polyfill。例如:仅限我的
replaceAll()。
标签: reactjs typescript create-react-app tsconfig