更新于 2026-08-30

根据 JSON 生成 TypeScript 类型声明:type 还是 interface?

生成的类型是 <code>interface</code>,可声明合并、报错更友好;若你需要联合类型或映射类型,可改写成 <code>type</code>。两者在大多数业务场景下等价。

默认产物(interface)

export interface Root {
  id: number;
  name: string;
  active: boolean;
  tags: string[];
  profile: {
    city: string;
    followers: number;
  };
  scores: number[];
}

改成 type 也很简单

export type Root = {
  id: number;
  name: string;
  profile: { city: string; followers: number };
  scores: number[];
};

选择建议

常见问题

工具能直接输出 type 吗?

目前输出 interface,复制后把 interface X { 改成 type X = {' 即可,结构一致。

声明文件 .d.ts 能用吗?

能,把生成的 interface 放进 .d.ts,即为项目提供全局类型。

相关阅读

立即免费使用 JSON 转 Schema 工具 →