如何建模 JSON 对象

问题描述 投票:0回答:1

我想输入一个变量,该变量应该是一个能够使用

JSON.stringify
进行序列化的对象。

我找到了这个定义

export type JSONObject = { [key: string]: JSON }
export interface JSONArray extends Array<JSON> {}
export type JsonValue = null | string | number | boolean | JSONArray | JSONObject

但是有一些内置类型,或者更好的方法吗?

json typescript
1个回答
6
投票

没有内置类型,但从 Typescript 3.7 开始可以简化为:

type Json = string | number | boolean | null | Json[] | { [key: string]: Json };

有关递归类型别名的更多信息此处

© www.soinside.com 2019 - 2024. All rights reserved.