[英]object shorthand syntax in typescript compiler
interface o {
name: string
}
const func = (obj: o): boolean => true
// this should throw error message.(or warning message at least, but it doesn't)
func({ name })
name
is undefined in the code I wrote, so func({ name })
should throw an error I think. name
在我编写的代码中未定义,因此func({ name })
应该引发错误。 Is this intended? 这是故意的吗?
Can I fix this with eslint or tsc config? 我可以使用eslint或tsc config修复此问题吗?
edit : this isn't a duplicate 编辑 :这不是重复
I'm in node and Global.name is undefined. 我在节点中 ,Global.name未定义。
It seems tsc
thinks name
is string though. 似乎
tsc
认为name
是字符串。
my tsconfig.json: 我的tsconfig.json:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipLibCheck" : true,
"rootDir": "./src",
"outDir": "./src/js"
}
}
TypeScript declares a global called name
in lib.dom.d.ts
because of window.name
. 由于
window.name
TypeScript在lib.dom.d.ts
声明了一个全局的name
。
name
is declared with type never
but that doesn't prevent it from being an issue. name
以never
声明的类型声明,但这不会阻止它成为问题。 There is a discussion on Github on changing it's type to void
in which case there would be an error in your example. 关于Github的讨论是将其类型更改为
void
在这种情况下,示例中将出现错误。 A workaround is to configure ESLint with the rule no-restricted-globals
. 一种解决方法是使用规则
no-restricted-globals
配置ESLint。
However, since you're compiling for Node.js, there is actually a much better solution. 但是,由于要为Node.js进行编译,因此实际上有一个更好的解决方案。 Because you don't need any type definitions for DOM APIs it's best to not include
lib.dom.d.ts
at all. 因为您不需要DOM API的任何类型定义,所以最好根本不包含
lib.dom.d.ts
You can do this by specifying the lib
compiler option explicitly in tsconfig.json
: 您可以通过在
tsconfig.json
显式指定lib
编译器选项来执行此tsconfig.json
:
{
"compilerOptions": {
"lib": [
"ES5"
]
}
}
Note: the exact value for
lib
you'll need depends on your project.注意:所需的
lib
确切值取决于您的项目。 See the compiler options documentation for all available values and the defaults for different targets有关所有可用值和不同目标的默认值,请参见编译器选项文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.