命名空间'Intl'没有导出的成员'LocalesArgument'



使用React和TypeScript,我试图键入一个将参数传递给Date.toLocaleString的函数,但当我尝试使用Intl.LocalesArgument时,我收到一个错误,说Namespace 'Intl' has no exported member 'LocalesArgument'。我的tsconfig.json在下面,我尝试将target更改为es2020并重新启动开发服务器,但没有成功。

{
"compilerOptions": {
"target": "es5",
"lib": [
"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",
"useUnknownInCatchVariables": false
},
"include": [
"src"
]
}

我使用TypeScript4.6.3,升级到最新版本(撰写本文时为4.8.2(解决了此问题。

如果您无法更新版本,可以通过转到tsconfig并添加"ES2020.Intl";到您的";Lib";阵列:

"compilerOptions": {
...
"lib": [<other libs>, "ES2020.Intl"]
...
}

我刚刚在一个旧的TS版本上遇到了这个问题,并通过在types目录下添加index.d.ts来修复它:

declare namespace Intl {
type LocalesArgument =
| UnicodeBCP47LocaleIdentifier
| Locale
| readonly (UnicodeBCP47LocaleIdentifier | Locale)[]
| undefined
}

然后将CCD_ 11添加到CCD_ 13中的CCD_。

最新更新