From 43db4546dcd4aa595584b8b4e9d821d8dc5fa5a6 Mon Sep 17 00:00:00 2001 From: XiNiHa Date: Mon, 14 Nov 2022 06:16:13 +0900 Subject: [PATCH 1/3] Make brandWithType return the branded object --- packages/core/src/utils/index.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/core/src/utils/index.ts b/packages/core/src/utils/index.ts index 3f9b69469..5e0466b16 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 typeof typeBrandKey + ? OutputType + : T[K]; + } + : undefined; + +export function brandWithType( + val: T, + type: OutputType, +): BrandWithTypeResult { if (typeof val !== 'object' || val === null) { - return; + return undefined as BrandWithTypeResult; } Object.defineProperty(val, typeBrandKey, { enumerable: false, value: type, }); + + return val as BrandWithTypeResult; } export function getTypeBrand(val: unknown) { From dad1ebf6829074da4ba7e8b49f4572f4d607309d Mon Sep 17 00:00:00 2001 From: XiNiHa Date: Mon, 14 Nov 2022 06:20:03 +0900 Subject: [PATCH 2/3] Add changeset --- .changeset/dull-buses-fix.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/dull-buses-fix.md 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 From 68c08588194ca03f1e7a1cb7c97a481c794c201a Mon Sep 17 00:00:00 2001 From: Iha Shin Date: Mon, 14 Nov 2022 14:52:32 +0900 Subject: [PATCH 3/3] fix --- packages/core/src/utils/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/core/src/utils/index.ts b/packages/core/src/utils/index.ts index 5e0466b16..1c4028237 100644 --- a/packages/core/src/utils/index.ts +++ b/packages/core/src/utils/index.ts @@ -70,18 +70,18 @@ builder.objectType('MyObject', { type BrandWithTypeResult = T extends Record ? { - [K in keyof T & typeof typeBrandKey]: K extends typeof typeBrandKey - ? OutputType - : T[K]; + [K in keyof T | typeof typeBrandKey]: K extends keyof T + ? T[K] + : OutputType } - : undefined; + : T; export function brandWithType( val: T, type: OutputType, ): BrandWithTypeResult { if (typeof val !== 'object' || val === null) { - return undefined as BrandWithTypeResult; + return val as BrandWithTypeResult; } Object.defineProperty(val, typeBrandKey, {