typescript 打字稿中的通用对象类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40641370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:00:54  来源:igfitidea点击:

Generic Object type in typescript

typescripttypes

提问by Nahush Farkande

In typescript is there any way to assign a variable a generic object type. Here's what I mean by 'generic Object type'

在打字稿中有什么方法可以为变量分配一个通用对象类型。这就是我所说的“通用对象类型”的意思

let myVariable: GenericObject = 1 // Should throw an error
                              = 'abc' // Should throw an error
                              = {} // OK
                              = {name: 'qwerty'} //OK

i.e. It should only allow javascript objects to be assigned to the variable and no other type of data(number, string, boolean)

即它应该只允许将 javascript 对象分配给变量而不是其他类型的数据(数字、字符串、布尔值)

回答by Nitzan Tomer

Sure thing:

肯定的事:

type GenericObject = { [key: string]: any };

let myVariable1: GenericObject = 1; // Type 'number' is not assignable to type '{ [key: string]: any; }'
let myVariable2: GenericObject = 'abc'; // Type 'string' is not assignable to type '{ [key: string]: any; }'
let myVariable3: GenericObject = {} // OK
let myVariable4: GenericObject = {name: 'qwerty'} //OK

(code in playground)

操场上的代码

回答by JaredMcAteer

Typescript 2.1+ also has has a utility type, Record<K, T>, you can use instead of making your own definition

Typescript 2.1+ 也有一个实用程序类型, Record<K, T>,你可以使用而不是自己定义

const myObj: Record<string, any>;

I like to use the style described in top answer when I can give a meaningful name to keybut if it's not really as obvious or necessary Recordis a great option.

当我可以给出一个有意义的名字时,我喜欢使用 top answer 中描述的样式,key但如果它不是那么明显或必要的话,这Record是一个很好的选择。

回答by Bill Barnes

As of TypeScript 2.2 you can use

从 TypeScript 2.2 开始,您可以使用

let myVariable: object;

Edit: Here's an example:

编辑:这是一个例子:

let myVariable: object = { fun: 1 };

回答by Josh Leslie

A bit of a tangent since I haven't found a similar answer elsewhere, from @JaredMcAteer here, using recordhelped me with mixing enums + objects.

有点切线,因为我没有在其他地方找到类似的答案,来自 @JaredMcAteer 在这里,使用record帮助我混合了枚举 + 对象。

enum FOO_ENUM {
  BAR = 'BAZ';
}

type FOO_OBJECT_TYPE = { ... };

const BIZ_OBJECT: Record<FOO_ENUM, FOO_OBJECT_TYPE> = {
  [FOO_ENUM.BAR]: { ... }
}

Where before I was typing BIZ_OBJECTas
BIZ_OBJECT: {[type: string]: FOO_OBJECT}
which allowed something like BIZ_OBJECT.asd, now only a key from FOO_ENUMcan be used, e.g.

在我输入BIZ_OBJECTas
BIZ_OBJECT: {[type: string]: FOO_OBJECT}
允许类似的地方之前BIZ_OBJECT.asd,现在只能使用一个键 from FOO_ENUM,例如

  • BIZ_OBJECT.BAZ // { ... }
  • BIZ_OBJECT.asd // Property 'asd' does not exist on type ...
  • BIZ_OBJECT[FOO_ENUM.BAR] // { ... }
  • BIZ_OBJECT[FOO_ENUM.asd] // Property 'asd' does not ...
  • BIZ_OBJECT[FOO_ENUM['asd']] // ! not caught !
  • BIZ_OBJECT.BAZ // { ... }
  • BIZ_OBJECT.asd // Property 'asd' does not exist on type ...
  • BIZ_OBJECT[FOO_ENUM.BAR] // { ... }
  • BIZ_OBJECT[FOO_ENUM.asd] // Property 'asd' does not ...
  • BIZ_OBJECT[FOO_ENUM['asd']] // ! not caught !