Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 映射是否可以将键作为自定义类型?_Typescript_Typescript2.0 - Fatal编程技术网

Typescript 映射是否可以将键作为自定义类型?

Typescript 映射是否可以将键作为自定义类型?,typescript,typescript2.0,Typescript,Typescript2.0,我有自定义接口类型: export interface IDictionary { id: number; dbVersion: number; code: string; tableName: string; name: Name; shortName: ShortName; active: boolean; restricted: boolean; hierarchy: boolean; metadata: st

我有自定义接口类型:

export interface IDictionary {
    id: number;
    dbVersion: number;
    code: string;
    tableName: string;
    name: Name;
    shortName: ShortName;
    active: boolean;
    restricted: boolean;
    hierarchy: boolean;
    metadata: string;
    rights: any[];
}
我试图构建Map():

其中节点是对象:

{ name: { ru: "Root1"}}
我应该得到:
[{name:{ru:{Papa}}]

在JavaScript中(作为扩展,TypeScript),没有两个对象是相等的,除非它们引用同一个对象。如果创建一个新对象,它不会认为它等于任何现有的对象。

因为在查找元素时,地图考虑了这样的相等性,如果以一个对象存储一个值作为一个键,那么如果您再次将完全相同的对象引用作为一个键传递,则只能再次获得值:

interface Name {
  ru: string
}
interface ShortName extends Name {}
interface IDictionary {
  id?: number;
  dbVersion?: number;
  code?: string;
  tableName?: string;
  name: Name;
  shortName?: ShortName;
  active?: boolean;
  restricted?: boolean;
  hierarchy?: boolean;
  metadata?: string;
  rights?: any[];
}

const node = { name: { ru: 'Root1' } }

const dataMap = new Map<IDictionary, IDictionary[]>([[
  node, [{ name: { ru: 'Papa' } }]
]])

console.log(dataMap.get({ name: { ru: 'Root1' } })) // undefined
console.log(dataMap.get(node))                      // [ { name: { ru: 'Papa' } } ]
接口名称{
鲁:字符串
}
接口ShortName扩展名称{}
接口词典{
id?:编号;
dbVersion?:编号;
代码?:字符串;
tableName?:字符串;
姓名:姓名;
shortName?:shortName;
活动?:布尔值;
限制?:布尔;
层次?:布尔;
元数据?:字符串;
权利?:任何[];
}
const node={name:{ru:'Root1'}}
const dataMap=新映射([[
节点,[{name:{ru:'Papa'}}]
]])
console.log(dataMap.get({name:{ru:'Root1'}}))//未定义
console.log(dataMap.get(node))/[{name:{ru:'Papa'}]

{ name: { ru: "Root1"}}
interface Name {
  ru: string
}
interface ShortName extends Name {}
interface IDictionary {
  id?: number;
  dbVersion?: number;
  code?: string;
  tableName?: string;
  name: Name;
  shortName?: ShortName;
  active?: boolean;
  restricted?: boolean;
  hierarchy?: boolean;
  metadata?: string;
  rights?: any[];
}

const node = { name: { ru: 'Root1' } }

const dataMap = new Map<IDictionary, IDictionary[]>([[
  node, [{ name: { ru: 'Papa' } }]
]])

console.log(dataMap.get({ name: { ru: 'Root1' } })) // undefined
console.log(dataMap.get(node))                      // [ { name: { ru: 'Papa' } } ]