使用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_。