Typescript 如何创建JSON格式的接口?

Typescript 如何创建JSON格式的接口?,typescript,typescript2.0,Typescript,Typescript2.0,如何在TypeScript中为此模式JSON创建接口: { "1": [1,2,3], "55": [1,3,68] } 我试过: interface IJson { number: number[] } 看起来您需要一个here,因为您需要一个具有任意数字键的类型。所以这看起来像: interface IJson { [key: number]: number[]; } // This assignment is okay: const test: IJSON = {

如何在TypeScript中为此模式JSON创建接口:

{
  "1": [1,2,3],
  "55": [1,3,68]
}
我试过:

interface IJson {
  number: number[]
}
看起来您需要一个here,因为您需要一个具有任意数字键的类型。所以这看起来像:

interface IJson {
    [key: number]: number[];
}

// This assignment is okay:
const test: IJSON = {
    "1": [1, 2, 3],
    "55": [1, 3, 68],
}

// This is also okay and someValue is inferred to have type number[]
const someValue = test[1]

// But be careful, using this, only numbers can index the type:
// See the below note for how to allow string indices instead.
const otherValue = test["55"] // Error: Index expression is not of type number.
请注意,如果字符串更适合您的用例,那么您也可以使用字符串作为索引签名,而不是数字。只需将
key:number
替换为
key:string

看起来您需要一个here,因为您需要一个具有任意数字键的类型。所以这看起来像:

interface IJson {
    [key: number]: number[];
}

// This assignment is okay:
const test: IJSON = {
    "1": [1, 2, 3],
    "55": [1, 3, 68],
}

// This is also okay and someValue is inferred to have type number[]
const someValue = test[1]

// But be careful, using this, only numbers can index the type:
// See the below note for how to allow string indices instead.
const otherValue = test["55"] // Error: Index expression is not of type number.

请注意,如果字符串更适合您的用例,那么您也可以使用字符串作为索引签名,而不是数字。只需将
key:number
替换为
key:string

您可以将key设置为字符串或数字,也可以将map的值设置为多种类型

interface IJSON {
    [key: string]: (string | number)[];
    [index: number]: (string | number)[];
}
const test: IJSON = {
    "1": [1, 2, 3],
    "55": [1, 3, 68],
    525: [1, 3, 68],
}
如果您想拥有更通用的内容,可以对索引持有的值类型使用通用

interface IJSON2<TValue> {
    [key: string]: TValue[];
    [index: number]: TValue[];
}
const test2: IJSON2<number> = {
    "1": [1, 2, 3],
    "55": [1, 3, 68]
}
接口IJSON2{
[键:字符串]:TValue[];
[索引:编号]:TValue[];
}
常量test2:IJSON2={
"1": [1, 2, 3],
"55": [1, 3, 68]
}

您可以将键设置为字符串或数字,也可以将映射的值设置为多种类型

interface IJSON {
    [key: string]: (string | number)[];
    [index: number]: (string | number)[];
}
const test: IJSON = {
    "1": [1, 2, 3],
    "55": [1, 3, 68],
    525: [1, 3, 68],
}
如果您想拥有更通用的内容,可以对索引持有的值类型使用通用

interface IJSON2<TValue> {
    [key: string]: TValue[];
    [index: number]: TValue[];
}
const test2: IJSON2<number> = {
    "1": [1, 2, 3],
    "55": [1, 3, 68]
}
接口IJSON2{
[键:字符串]:TValue[];
[索引:编号]:TValue[];
}
常量test2:IJSON2={
"1": [1, 2, 3],
"55": [1, 3, 68]
}

如果您对数组中的元素数量没有特殊要求,可以使用
类型IJSON=Record
Record
是一种内置类型。

如果您对数组中的元素数量没有特殊要求,可以使用
type IJSON=Record
<代码>记录是内置类型。

如何初始化?我做到了:
public-obj:IJson={}
然后尝试
此.obj[key].push(值)
它不能插入值空对象
{}
没有要开始的键,因此
这个.obj[key]
可能是
未定义的
。您必须先检查它是否存在,如果不存在,则将其设置为空数组,然后再开始将内容推送到它。IE:
如果(!this.obj[key])this.obj[key]=[];这个.obj[key].push(value)
。所以,key只能是数字?不是绳子?或者我可以设置自定义类型键吗?该键只能是字符串或数字(但不能同时是字符串和数字)。要使其成为字符串,请在类型定义中将
key:number
替换为
key:string
。您无法设置自定义密钥类型(遗憾的是)。什么是
U
?字段
obj
的类型或
obj
中值的类型?@PatrickDesjardins是否编辑该案例的地址?以及如何初始化?我做到了:
public-obj:IJson={}
然后尝试
此.obj[key].push(值)
它不能插入值空对象
{}
没有要开始的键,因此
这个.obj[key]
可能是
未定义的
。您必须先检查它是否存在,如果不存在,则将其设置为空数组,然后再开始将内容推送到它。IE:
如果(!this.obj[key])this.obj[key]=[];这个.obj[key].push(value)
。所以,key只能是数字?不是绳子?或者我可以设置自定义类型键吗?该键只能是字符串或数字(但不能同时是字符串和数字)。要使其成为字符串,请在类型定义中将
key:number
替换为
key:string
。您无法设置自定义密钥类型(遗憾的是)。什么是
U
?字段
obj
的类型或
obj
中值的类型?@PatrickDesjardins是否编辑该案例的地址?