From ec283b7a644026364cbb5ab9db1c1bd3427024f5 Mon Sep 17 00:00:00 2001 From: Akash R Date: Mon, 2 Mar 2026 21:40:07 +0530 Subject: [PATCH] Fix optional TypeScript reducer params --- crates/bindings-typescript/package.json | 2 +- .../src/lib/type_builders.test-d.ts | 2 ++ .../src/lib/type_builders.ts | 21 ++++++++++++++++--- .../bindings-typescript/src/server/views.ts | 7 +++++-- .../tsconfig.typecheck.json | 10 +++------ crates/bindings-typescript/vitest.config.ts | 2 +- 6 files changed, 30 insertions(+), 14 deletions(-) diff --git a/crates/bindings-typescript/package.json b/crates/bindings-typescript/package.json index 68d23327193..5789b3182ef 100644 --- a/crates/bindings-typescript/package.json +++ b/crates/bindings-typescript/package.json @@ -29,7 +29,7 @@ "format": "prettier . --write --ignore-path ../../.prettierignore", "lint": "eslint . && prettier . --check --ignore-path ../../.prettierignore", "test": "vitest run", - "test:typecheck": "vitest typecheck --run", + "test:typecheck": "tsc -p tsconfig.typecheck.json --noEmit", "coverage": "vitest run --coverage", "brotli-size": "brotli-size dist/index.js", "size": "pnpm -s build && size-limit", diff --git a/crates/bindings-typescript/src/lib/type_builders.test-d.ts b/crates/bindings-typescript/src/lib/type_builders.test-d.ts index d0595f25829..bae3089e145 100644 --- a/crates/bindings-typescript/src/lib/type_builders.test-d.ts +++ b/crates/bindings-typescript/src/lib/type_builders.test-d.ts @@ -35,6 +35,8 @@ const rowOptionOptional = { }; type RowOptionOptional = InferTypeOfRow; // eslint-disable-next-line @typescript-eslint/no-unused-vars +const _rowOptionOptionalOmitted: RowOptionOptional = {}; +// eslint-disable-next-line @typescript-eslint/no-unused-vars const _rowOptionOptionalNone: RowOptionOptional = { foo: undefined, }; diff --git a/crates/bindings-typescript/src/lib/type_builders.ts b/crates/bindings-typescript/src/lib/type_builders.ts index 6bf8ae1ec99..4579d2b3a9e 100644 --- a/crates/bindings-typescript/src/lib/type_builders.ts +++ b/crates/bindings-typescript/src/lib/type_builders.ts @@ -36,12 +36,27 @@ export type Infer = T extends RowObj ? InferTypeOfTypeBuilder : never; +type OptionalRowKeys = { + [K in keyof T & string]-?: CollapseColumn extends OptionBuilder + ? K + : never; +}[keyof T & string]; + +type RequiredRowKeys = Exclude< + keyof T & string, + OptionalRowKeys +>; + /** * Helper type to extract the type of a row from an object. */ -export type InferTypeOfRow = { - [K in keyof T & string]: InferTypeOfTypeBuilder>; -}; +export type InferTypeOfRow = Prettify< + { + [K in RequiredRowKeys]: InferTypeOfTypeBuilder>; + } & { + [K in OptionalRowKeys]?: InferTypeOfTypeBuilder>; + } +>; /** * Helper type to extract the type of a row from an object. diff --git a/crates/bindings-typescript/src/server/views.ts b/crates/bindings-typescript/src/server/views.ts index accd0c92563..02949da6f86 100644 --- a/crates/bindings-typescript/src/server/views.ts +++ b/crates/bindings-typescript/src/server/views.ts @@ -200,8 +200,11 @@ type ViewInfo = { returnTypeBaseSize: number; }; -export type Views = ViewInfo>[]; -export type AnonViews = ViewInfo>[]; +type AnyViewFn = (ctx: ViewCtx, params: any) => any; +type AnyAnonymousViewFn = (ctx: AnonymousViewCtx, params: any) => any; + +export type Views = ViewInfo[]; +export type AnonViews = ViewInfo[]; // A helper to get the product type out of a type builder. // This is only non-never if the type builder is an array. diff --git a/crates/bindings-typescript/tsconfig.typecheck.json b/crates/bindings-typescript/tsconfig.typecheck.json index 4ff6d6ca220..48247e80481 100644 --- a/crates/bindings-typescript/tsconfig.typecheck.json +++ b/crates/bindings-typescript/tsconfig.typecheck.json @@ -4,17 +4,13 @@ "jsx": "react-jsx" }, "include": [ - "tests/**/*", - "src/lib/**/*", - "src/sdk/**/*", - "src/react/**/*", - "test-app/**/*" + "src/**/*.test-d.ts", + "src/server/sys.d.ts" ], "exclude": [ "node_modules", "dist/**/*", "src/svelte/**/*", - "src/vue/**/*", - "src/server/**/*" + "src/vue/**/*" ] } diff --git a/crates/bindings-typescript/vitest.config.ts b/crates/bindings-typescript/vitest.config.ts index 56dd6752bb9..44134dda93b 100644 --- a/crates/bindings-typescript/vitest.config.ts +++ b/crates/bindings-typescript/vitest.config.ts @@ -7,7 +7,7 @@ export default defineConfig({ globals: true, environment: 'node', typecheck: { - include: ['tests/**/*.test.ts'], + include: ['src/**/*.test-d.ts'], tsconfig: './tsconfig.typecheck.json', }, },