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

TypeScript同一接口内条件属性的实现可行性咨询

TypeScript 实现命令解析模式的条件属性约束

当然可以实现你的需求,核心是通过联合类型+分支定义来约束属性的存在性,你的泛型写法失效是因为默认值导致TypeScript无法精准收窄类型。

最直观的实现方式

直接定义两种模式对应的独立类型,再合并为联合类型,明确每种模式的属性规则:

type CommandParsingMode = "routes" | "custom";

// routes模式:仅包含commandParsingMode字段
type RoutesModeConfig = {
  commandParsingMode: "routes";
};

// custom模式:必须包含directoryPaths字段
type CustomModeConfig = {
  commandParsingMode: "custom";
  directoryPaths: {
    // 这里填入你的directoryPaths结构,示例如下
    srcDir: string;
    outputDir: string;
  };
};

// 最终的Type是两种模式的联合
type Type = RoutesModeConfig | CustomModeConfig;

效果验证

TypeScript会自动完成类型收窄:

  • 当commandParsingMode设为"routes"时,编辑器会提示directoryPaths不存在
  • 当设为"custom"时,必须提供符合结构的directoryPaths属性

在函数中使用时,类型收窄会自动生效:

function processConfig(config: Type) {
  if (config.commandParsingMode === "custom") {
    // 此处可直接访问config.directoryPaths,类型已自动收窄
    console.log(config.directoryPaths.srcDir);
  } else {
    // 此处config为RoutesModeConfig,无directoryPaths属性
    console.log("使用routes解析模式");
  }
}

修复你的泛型写法

如果坚持用泛型实现,需要去掉默认泛型参数,确保TypeScript能明确识别具体模式:

type CommandParsingMode = "routes" | "custom";
type BaseProps = {
  // 填入你的BaseProps内容,示例如下
  appName: string;
};

type Props<M extends CommandParsingMode> = BaseProps & {
  commandParsingMode: M;
} & (M extends "custom" ? { directoryPaths: { srcDir: string; outputDir: string } } : {});

// 使用时必须明确指定泛型参数
const routesConfig: Props<"routes"> = {
  appName: "my-cli",
  commandParsingMode: "routes"
  // 此处添加directoryPaths会直接报错
};

const customConfig: Props<"custom"> = {
  appName: "my-cli",
  commandParsingMode: "custom",
  directoryPaths: { srcDir: "./src", outputDir: "./dist" }
};

你原写法中= CommandParsingMode的默认值会让TypeScript认为泛型参数可能是两种模式的联合,导致条件分支无法生效,去掉默认值后就能实现预期约束。

内容的提问来源于stack exchange,提问作者Shockch4rge

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 20:16:11