diff --git a/.changeset/dull-buses-fix.md b/.changeset/dull-buses-fix.md new file mode 100644 index 000000000..60a3ff7f6 --- /dev/null +++ b/.changeset/dull-buses-fix.md @@ -0,0 +1,5 @@ +--- +'@pothos/core': minor +--- + +Make `brandWithType` return the branded object diff --git a/packages/core/src/utils/index.ts b/packages/core/src/utils/index.ts index 3f9b69469..1c4028237 100644 --- a/packages/core/src/utils/index.ts +++ b/packages/core/src/utils/index.ts @@ -68,15 +68,28 @@ builder.objectType('MyObject', { } } -export function brandWithType(val: unknown, type: OutputType) { +type BrandWithTypeResult = T extends Record + ? { + [K in keyof T | typeof typeBrandKey]: K extends keyof T + ? T[K] + : OutputType + } + : T; + +export function brandWithType( + val: T, + type: OutputType, +): BrandWithTypeResult { if (typeof val !== 'object' || val === null) { - return; + return val as BrandWithTypeResult; } Object.defineProperty(val, typeBrandKey, { enumerable: false, value: type, }); + + return val as BrandWithTypeResult; } export function getTypeBrand(val: unknown) {