将 json 数据转化为 ts 的 types 结构
use npm:
npm i --save-dev json-ts-types
use yarn:
yarn add -D json-ts-types
数据:
const data = {
data: [
{
id: "xxx",
type: "text",
name: "name",
enabled: true,
corp_id: "xxx",
is_default: true,
create_ts: 1607413075,
update_ts: 1614065954,
},
{
id: "xxx",
type: "text",
name: "tb_nick",
enabled: true,
corp_id: "xxx",
is_default: true,
create_ts: 1608172135,
update_ts: 1620440673,
},
{
id: "xxx",
type: "text",
name: "jd_nick",
enabled: true,
corp_id: "xxx",
is_default: true,
create_ts: 1620985673,
update_ts: 1621410474,
},
],
};
例子:
const jtt = new JsonTsTypes();
console.log(jtt.formart(data));
输出:
export type TDemo = {
data?: {
id?: string;
type?: string;
name?: string;
enabled?: boolean;
corp_id?: string;
is_default?: boolean;
create_ts?: number;
update_ts?: number;
}[];
};
初始化:
type TJTTOptions = {
// 缩进单位,值为空格数, 默认2
indentUnit?: number;
// 是否使用tab缩进, 默认false
indentWithTabs?: boolean;
// 是否导出 默认 export
useExport?: boolean;
};
const jtt = newJsonTsTypes(options?: TJTTOptions)
使用:
const result = jtt.formart(data, name?: string, options?: TJTTOptions)