From 2b2d4be308a704c621cf241ec70dca3588551baa Mon Sep 17 00:00:00 2001 From: captain-mirage <241807724+captain-mirage@users.noreply.github.com> Date: Mon, 21 Sep 2026 15:14:31 -0400 Subject: [PATCH] ts-sdk: stop Prettify from rewriting Uuid and branded primitives `Prettify` maps every type it is given. `Uuid` is missing from `DoNotPrettify`, so a `t.uuid()` column infers as a structural record of `Uuid`'s members, and `Prettify` becomes a record of `String`'s members that is no longer assignable to `string`. Add `Uuid` to `DoNotPrettify` and pass primitives, branded or not, through unchanged. The primitive check sits inside a `T extends object` branch so a generic `Prettify` stays spreadable, as `sdk-test-ts` relies on. --- .../src/lib/type_util.test-d.ts | 64 +++++++++++++++++++ .../bindings-typescript/src/lib/type_util.ts | 19 ++++-- 2 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 crates/bindings-typescript/src/lib/type_util.test-d.ts diff --git a/crates/bindings-typescript/src/lib/type_util.test-d.ts b/crates/bindings-typescript/src/lib/type_util.test-d.ts new file mode 100644 index 00000000000..d6720e77c63 --- /dev/null +++ b/crates/bindings-typescript/src/lib/type_util.test-d.ts @@ -0,0 +1,64 @@ +import type { ConnectionId } from './connection_id'; +import type { Identity } from './identity'; +import type { Prettify } from './type_util'; +import type { TimeDuration } from './time_duration'; +import type { Timestamp } from './timestamp'; +import type { Uuid } from './uuid'; + +declare const brand: unique symbol; + +/** + * A branded primitive: an intersection of a primitive with a marker object. + * `Prettify` must leave it alone — mapping it produces a structural record of + * `String`'s members that no longer satisfies `string`. + */ +type UserId = string & { readonly [brand]: 'UserId' }; + +declare const userId: Prettify; +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const _brandedIsStillAString: string = userId; +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const _brandedKeepsItsBrand: UserId = userId; + +// Plain primitives round-trip unchanged. +declare const str: Prettify; +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const _str: string = str; +declare const num: Prettify; +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const _num: number = num; +declare const big: Prettify; +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const _big: bigint = big; + +// Every SATS wrapper class is passed through as the class, not as a +// structural record of its members. +declare const identity: Prettify; +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const _identity: Identity = identity; +declare const connectionId: Prettify; +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const _connectionId: ConnectionId = connectionId; +declare const timestamp: Prettify; +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const _timestamp: Timestamp = timestamp; +declare const timeDuration: Prettify; +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const _timeDuration: TimeDuration = timeDuration; +declare const uuid: Prettify; +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const _uuid: Uuid = uuid; + +// Object types are still flattened. +type Intersected = { a: string } & { b: number }; +declare const flattened: Prettify; +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const _flattened: { a: string; b: number } = flattened; + +// A generic row stays spreadable, as reducer args are in `sdk-test-ts`. +// eslint-disable-next-line @typescript-eslint/no-unused-vars +function _spreadGenericRow>( + row: Prettify<{ [K in keyof Row]: Row[K] }> +) { + return { ...row }; +} diff --git a/crates/bindings-typescript/src/lib/type_util.ts b/crates/bindings-typescript/src/lib/type_util.ts index 35156348c4d..a89410debf3 100644 --- a/crates/bindings-typescript/src/lib/type_util.ts +++ b/crates/bindings-typescript/src/lib/type_util.ts @@ -3,20 +3,31 @@ import type { Identity } from './identity'; import type { ScheduleAt } from './schedule_at'; import type { TimeDuration } from './time_duration'; import type { Timestamp } from './timestamp'; +import type { Uuid } from './uuid'; type DoNotPrettify = | Identity | ConnectionId | Timestamp | TimeDuration - | ScheduleAt; + | ScheduleAt + | Uuid; + +type Primitive = string | number | boolean | bigint | symbol | null | undefined; /** * Utility to make TS show cleaner types by flattening intersections. + * + * Primitives pass through unchanged. A branded primitive (`string & Brand`) + * is an object type, but mapping it yields a structural record of `String`'s + * members that no longer satisfies `string`. The outer `object` check keeps a + * generic `Prettify` spreadable. */ -export type Prettify = T extends DoNotPrettify - ? T - : { [K in keyof T]: T[K] } & {}; +export type Prettify = T extends object + ? T extends DoNotPrettify | Primitive + ? T + : { [K in keyof T]: T[K] } & {} + : T; /** * Helper function to sets a field in an object