简体   繁体   English

Typescript编译器中的对象简写语法

[英]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. namenever声明的类型声明,但这不会阻止它成为问题。 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.

相关问题 打字稿在速记构造函数中创建对象 - Typescript create object in shorthand constructor TypeScript中的object参数映射到另一个object有简写吗? - Is there a shorthand for mapping object parameters to another object in TypeScript? Typescript 参数作为 object 传递时的构造函数简写 - Typescript constructor shorthand when parameters are passed as an object Typescript对象迭代编译问题 - Typescript object iteration compiler problems TypeScript - 将一个对象的所有属性分配给另一个对象的速记方式 - TypeScript - Shorthand way to assign all properties of one object to another Typescript Compiler API - 提取导出的 Typescript 对象 - Typescript Compiler API - Extract Exported Typescript Object 在 object 的某个键处将项目推送或创建到数组的简写语法 - Shorthand syntax to push or create item to array, at certain key of object Typescript object 可选的初始值设定项错误,默认属性在构造函数简写中声明 - Typescript object initializer error for optional with default property declared in constructor shorthand Vue.js - Typescript:对象文字中的预期方法简写 - Vue.js - Typescript: Expected method shorthand in object literal TypeScript 的字符串枚举或 object 文字是否有(更好的)速记? - Is there a (better) shorthand for TypeScript's string enums or object literals?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM