diff --git a/crates/bindings-typescript/package.json b/crates/bindings-typescript/package.json index c7bef474278..c294fc5e6c6 100644 --- a/crates/bindings-typescript/package.json +++ b/crates/bindings-typescript/package.json @@ -30,6 +30,7 @@ "lint": "eslint . && prettier . --check --ignore-path ../../.prettierignore", "test": "vitest run", "test:typecheck": "vitest typecheck --run", + "test:typecheck:dts": "tsc -p tsconfig.typecheck.dts.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..3d4e7232292 100644 --- a/crates/bindings-typescript/src/lib/type_builders.ts +++ b/crates/bindings-typescript/src/lib/type_builders.ts @@ -39,9 +39,24 @@ export type Infer = T extends RowObj /** * Helper type to extract the type of a row from an object. */ -export type InferTypeOfRow = { - [K in keyof T & string]: InferTypeOfTypeBuilder>; -}; +type OptionalRowKeys = { + [K in keyof T & string]-?: CollapseColumn extends OptionBuilder + ? K + : never; +}[keyof T & string]; + +type RequiredRowKeys = Exclude< + keyof T & string, + OptionalRowKeys +>; + +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/sdk/table_cache.ts b/crates/bindings-typescript/src/sdk/table_cache.ts index 42645daaecf..543d3fb78ed 100644 --- a/crates/bindings-typescript/src/sdk/table_cache.ts +++ b/crates/bindings-typescript/src/sdk/table_cache.ts @@ -123,7 +123,8 @@ export class TableCacheImpl< const columns = idx.columns; // Extract the tuple key for this btree index (column order preserved) - const getKey = (row: Row): readonly unknown[] => columns.map(c => row[c]); + const getKey = (row: Row): readonly unknown[] => + columns.map(c => (row as Record)[c]); // The server’s ranged scan fixes all prefix cols to equality and applies // the bound only to the *last* term. We mirror that. 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.dts.json b/crates/bindings-typescript/tsconfig.typecheck.dts.json new file mode 100644 index 00000000000..d908d6db47d --- /dev/null +++ b/crates/bindings-typescript/tsconfig.typecheck.dts.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "jsx": "react-jsx" + }, + "include": ["src/**/*.test-d.ts", "src/server/sys.d.ts"], + "exclude": ["node_modules", "dist/**/*", "src/svelte/**/*", "src/vue/**/*"] +}