簡體   English   中英

TypeScript:如何 model json object

[英]TypeScript: how to model a json object

我想輸入一個變量,它應該是一個 object 能夠用 JSON.stringify 序列化。

我找到了這個定義,但我想知道是否有一些內置類型,或者更好的方法:

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

在我看來,這應該是很常見的事情。

沒有內置類型,但從 Typescript 3.7 開始可以簡化為:

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

更多關於遞歸類型別名的信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM