
Dagger TypeScript SDK 中 TypeDefWithEnumValueOpts 类型别名解析枚举值 TypeDef 的可选参数【免费下载链接】daggerAutomation engine to build, test and ship any codebase. Runs locally, in CI, or directly in the cloud项目地址: https://gitcode.com/GitHub_Trending/da/dagger本文为 Dagger自动化构建与测试引擎TypeScript SDK 生成的 API 参考文档解读。TypeDefWithEnumValueOpts是dagger.io/dagger客户端库中api/client.gen模块导出的一个类型别名Type Alias用于描述在动态构建模块类型定义TypeDef时向枚举Enum追加静态值这一操作所接受的可选参数集合。读完本文你将掌握该类型的完整字段定义deprecated、description、sourceMap、它在TypeDef.withEnumValue()方法中的消费方式、与替代方法withEnumMember()的关系以及在实际代码中为枚举值补全文档字符串与源码映射的可复制示例。一、类型定位TypeDef 动态构建体系中的一环Dagger 的 TypeScript SDK 通过TypeDef这一对象化类型让模块可以以编程方式定义自己的 GraphQL 类型结构对象、接口、枚举、标量等。每个with*系列方法返回新的TypeDef形成链式构建器风格。TypeDefWithEnumValueOpts正是其中withEnumValue方法的第二个参数类型。从生成的源码 sdk/typescript/src/api/client.gen.ts 看该类型是一个全字段可选的对象字面量类型export type TypeDefWithEnumValueOpts { /** * A doc string for the value, if any */ description?: string /** * The source map for the enum value definition. */ sourceMap?: SourceMap /** * If deprecated, the reason or migration path. */ deprecated?: string }三个字段全部是可选的optional这意味着TypeDef.withEnumValue(STAGING)不带 opts 调用完全合法opts 仅用于为这个枚举值补充“元数据”文档注释、源码位置、弃用说明。文档页面包屑中的层级关系是dagger.io/dagger 模块 →api/client.gen模块 → 类型别名TypeDefWithEnumValueOpts。二、三个可选属性逐一解析1.deprecated?:stringIf deprecated, the reason or migration path.若已弃用说明弃用原因或迁移路径。类型string可选语义当某个枚举值被标记为弃用时填入弃用原因或建议的迁移路径文本该文本会体现在最终生成的模块 schema 中供后续调用方含 IDE 提示、文档生成器展示弃用告警。2.description?:stringA doc string for the value, if any该值的文档字符串如有。类型string可选语义为该枚举值写入一段文档字符串。它是后续dagger module introspect等工具为模块类型生成文档的基础内容建议用一句话说明该枚举值的业务含义。3.sourceMap?:SourceMapThe source map for the enum value definition.枚举值定义的源码映射。类型引用SourceMap类可选语义提供该枚举值定义在模块源码中的位置映射。SourceMap的完整类文档见 SourceMap 类参考。从源码结构看SourceMap用于把 schema 中的类型元素回溯到用户模块代码的具体文件与位置是 Dagger 模块自省与工具链如错误定位、代码生成的关键辅助信息。三个属性的组合使用方式均可省略const enumDef client.moduleDef .withEnum(Environment, { description: Deployment environments. }) .withEnumValue(STAGING, { description: The staging environment., deprecated: Use PRODUCTION for new pipelines., })三、消费方TypeDef.withEnumValue()的签名与内部机制该 opts 类型唯一的直接消费方是TypeDef类上的withEnumValue方法。生成的实现位于 client.gen.ts 第 15875-15886 行/** * Adds a static value for an Enum TypeDef, failing if the type is not an enum. * param value The name of the value in the enum * param opts.description A doc string for the value, if any * param opts.sourceMap The source map for the enum value definition. * param opts.deprecated If deprecated, the reason or migration path. * deprecated Use withEnumMember instead */ withEnumValue (value: string, opts?: TypeDefWithEnumValueOpts): TypeDef { const ctx this._ctx.select(withEnumValue, { value, ...opts }) return new TypeDef(ctx) }几个关键实现细节失败语义方法注释明确“Adds a static value for an Enum TypeDef, failing if the type is not an enum”——若当前TypeDef不是枚举种类操作会失败。因此正确用法是先withEnum(name, opts)创建枚举再链式withEnumValue(...)。懒式执行方法并不直接发起请求而是调用this._ctx.select(withEnumValue, { value, ...opts })生成一个选择节点opts中的三个字段被展开进查询参数。Dagger 客户端采用惰性lazy求值模型整个构建链在最终需要结果如导出模块定义时才提交执行。返回值返回新的TypeDef实例保证链式调用不破坏原有定义。弃用标记注意方法注释中的deprecated Use withEnumMember instead——withEnumValue已被标记弃用新代码应改用withEnumMember。四、弃用路径与withEnumMember/TypeDefWithEnumMemberOpts的对照与withEnumValue同文件定义的姊妹类型TypeDefWithEnumMemberOpts见 client.gen.ts 第 3154-3174 行多了一个字段export type TypeDefWithEnumMemberOpts { /** * The value of the member in the enum */ value?: string /** * A doc string for the member, if any */ description?: string /** * The source map for the enum member definition. */ sourceMap?: SourceMap /** * If deprecated, the reason or migration path. */ deprecated?: string }对应方法withEnumMember(name, opts?)client.gen.ts 第 15867-15873 行接收“成员名”作为第一个参数并通过opts.value单独携带“值”而withEnumValue的第一个参数value语义为“enum 中该值的名称”二者在命名上存在历史演进痕迹。从核心引擎侧看core/typedef.go 中NewEnumValueTypeDef已统一以EnumMemberTypeDef作为实现类型说明后端已将“枚举值/枚举成员”收敛为同一概念。迁移建议新代码直接改用withEnumMember仍在阅读旧模块或第三方代码时遇到TypeDefWithEnumValueOpts可将其理解为“带value命名差异的旧版枚举成员 opts”。五、完整实战示例下面是一个完整的链式用法演示创建枚举 → 追加两个枚举值一个带完整 opts一个不带→ 挂到模块定义上import { Client } from dagger.io/dagger async function main(client: Client) { const envEnum client.moduleDef .withEnum(Environment, { description: Deployment target environments., }) // 带 opts文档字符串 弃用说明 .withEnumMember(STAGING, { value: STAGING, description: Pre-production environment., }) // withEnumValue 的弃用等价写法保留以展示 TypeDefWithEnumValueOpts 用法 .withEnumValue(LEGACY, { description: Removed legacy environment., deprecated: Use STAGING instead; scheduled for removal., }) const def client.moduleDef .withName(mynamespace/env-demo) .withObject(Main, {}) // 将 TypeDef 作为模块的公开类型挂到模块定义 .withTypeDef(Environment, envEnum) await def.export(client.directory(/tmp/env-demo)) }要点回顾opts整体可选三个字段也各自可选最小调用withEnumValue(STAGING)即合法description与deprecated是纯字符串最终进入模块 schema 的文档与弃用元数据sourceMap需要构造SourceMap对象典型场景是把动态生成的类型定义回溯到模块源码位置供自省工具定位。六、相关类型速查类型/类用途参考位置TypeDefWithEnumValueOpts本篇主题withEnumValue的可选参数文档、源码TypeDefWithEnumMemberOpts后继者多value字段源码TypeDefWithEnumOptswithEnum创建枚举时的可选参数文档TypeDefWithFieldOpts对象字段 opts三字段与本类型同构源码SourceMap源码映射类sourceMap字段类型文档可以看到description/sourceMap/deprecated这一组“类型元素元数据”三字段在TypeDefWithFieldOpts、TypeDefWithEnumMemberOpts等各 opts 类型间保持了完全一致的结构——这是 Dagger SDK 代码生成器为类型定义体系设计的统一约定掌握TypeDefWithEnumValueOpts即可推广理解其余 opts 类型。【免费下载链接】daggerAutomation engine to build, test and ship any codebase. Runs locally, in CI, or directly in the cloud项目地址: https://gitcode.com/GitHub_Trending/da/dagger创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考