You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Typescript中如何基于interface自动生成带模拟值的对象

首先要明确一个基础限制:TypeScript 的 interface 只存在于编译阶段,编译为 JS 之后就会被完全擦除,运行时没办法直接读取 interface 的结构信息,所以没法直接基于 interface 直接在运行时动态生成对象。要实现新增字段自动同步的效果,有两种可行的方案:

方案一:Schema 驱动开发(推荐,实时生效无需额外构建步骤)

我们可以反过来先定义字段结构的 schema,再从 schema 自动推导得到 interface 类型,同时通过通用函数生成模拟对象。新增字段时只要修改 schema,类型和生成的对象会自动同步,不需要额外操作:

// 1. 定义字段和对应模拟值的生成规则,新增字段只需要改这里
const myInterfaceSchema = {
  first: () => "firstValue",
  second: () => "secondValue",
  // 示例新增字段,自动同步到类型和生成对象
  third: () => Math.random() * 100,
} as const;

// 2. 从 schema 自动推导 Interface 类型,不需要手动写 interface 定义
type MyInterface = {
  [K in keyof typeof myInterfaceSchema]: ReturnType<typeof myInterfaceSchema[K]>
};

// 3. 通用的 mock 生成函数,可以复用给所有 schema
function generateMock<T extends Record<string, () => any>>(schema: T): {[K in keyof T]: ReturnType<T[K]>} {
  return Object.fromEntries(
    Object.entries(schema).map(([key, generator]) => [key, generator()])
  ) as {[K in keyof T]: ReturnType<T[K]>};
}

// 生成的对象自动匹配 MyInterface 类型,新增字段自动包含
const myAutogeneratedObject: MyInterface = generateMock(myInterfaceSchema);

如果需要更真实的模拟数据,可以配合 @faker-js/faker 这类模拟库写生成函数,比如生成随机姓名、地址、手机号等。

方案二:编译时代码生成(适合不想改动原有 interface 定义的场景)

如果你要保留原有手动写 interface 的写法,可以基于 TypeScript 编译器 API 写一个小型代码生成脚本,在构建前自动扫描 interface 的字段生成对应对象代码。
示例脚本逻辑如下,可以把它加入你的项目构建流程,每次构建自动运行更新生成的对象:

// generate-mock.js 示例,需要提前安装 typescript 依赖
import * as ts from "typescript";
import * as fs from "fs";
import * as path from "path";

// 读取你定义 interface 的源文件
const sourcePath = path.resolve(__dirname, "./your-interface-file.ts");
const sourceFile = ts.createSourceFile(
  sourcePath,
  fs.readFileSync(sourcePath, "utf-8"),
  ts.ScriptTarget.Latest
);

// 提取 MyInterface 的所有字段名
const interfaceFields: string[] = [];
ts.forEachChild(sourceFile, (node) => {
  if (ts.isInterfaceDeclaration(node) && node.name.text === "MyInterface") {
    node.members.forEach(member => {
      if (ts.isPropertySignature(member) && ts.isIdentifier(member.name)) {
        interfaceFields.push(member.name.text);
      }
    });
  }
});

// 生成最终的 mock 对象代码
const outputContent = `
import type { MyInterface } from "./your-interface-file";

export const myAutogeneratedObject: MyInterface = {
  ${interfaceFields.map(field => `${field}: "${field}Value"`).join(",\n  ")}
};
`;

// 写入到输出文件
fs.writeFileSync(path.resolve(__dirname, "./generated-mock.ts"), outputContent);

内容的提问来源于stack exchange,提问作者Tao Gómez Gil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 13:15:04