我收到一个奇怪的错误 TS2339:类型“Y”上不存在属性“X”。我怎样才能解决这个问题?
我已将库添加到我的 'tsconfig.jsonc' 文件中:
"compilerOptions": {
"target": "es3", // "es3" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
"watch": true,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"lib": [
"esnext",
"es2018",
"es2017",
"es2016",
"es2015",
"es7",
"es6",
"es5",
"dom",
"dom.iterable",
"ScriptHost"
] /* Specify library files to be included in the compilation. */,
...
}
Run Code Online (Sandbox Code Playgroud)
编码
const userName: string = groupAddress.replace('@gmail.com', '')
Run Code Online (Sandbox Code Playgroud)
返回错误
'string' 类型不存在属性 'replace'。
相似地,
const addMembers = (email: string, studio: string, role): void => {
const memberKey = email.trim()
Run Code Online (Sandbox Code Playgroud)
返回
'string' 类型不存在属性 'trim'。
const groupKeys: string[] = [
`report.${name}@gmail.com`,
`support.${name}@gmail.com`
]
groupKeys.forEach((groupKey: string) => {
if (!GroupsApp.getGroupByEmail(groupKey).hasUser(memberKey)) {
AdminDirectory.Members.insert({ email: memberKey, role }, groupKey)
}
})
Run Code Online (Sandbox Code Playgroud)
返回
类型“{}”上不存在属性“forEach”。
我预计
但是打字稿说他们没有。
您的目标是 es3,但已告诉 TS,它拥有最新可用的所有库。如果没有 Polyfill,这是一个非常糟糕的主意。
TS 不会在功能上进行填充。它仅添加语法填充。所以说你是在给自己设下陷阱。删除库条目,将您的目标更新为现代目标,或者为其中的所有内容添加填充。
另外,如前所述,我希望您的 tsconfig 是 tsconfig.json,而不是 tsconfig.jsonc。否则那就是你的问题了。
| 归档时间: |
|
| 查看次数: |
9536 次 |
| 最近记录: |