Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/bindings-typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './time_duration';
export * from './timestamp';
export * from './utils';
export * from './identity';
export { default as t } from './server/type_builders';
97 changes: 97 additions & 0 deletions crates/bindings-typescript/src/server/type_builders.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { t, type AlgebraicTypeVariants } from '..';
import type {
I32ColumnBuilder,
I64ColumnBuilder,
InferTypeOfRow,
InferTypeOfTypeBuilder,
TypeBuilder,
} from './type_builders';

type MustBeNever<T> = [T] extends [never]
? true
: ['Error: Type must be never', T];

// Test type inference on a row
// i.e. a Record<string, TypeBuilder | ColumnBuilder> type
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const row = {
foo: t.string(),
bar: t.i32().primaryKey(),
idx: t.i64().index('btree').unique(),
};
type Row = InferTypeOfRow<typeof row>;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _row: Row = {
foo: 'hello',
bar: 42,
idx: 100n,
};

// Test that a row must not allow non-TypeBuilder or ColumnBuilder values
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const row2 = {
foo: {
// bar is not a TypeBuilder or ColumnBuilder, so this should fail
bar: t.string(),
},
bar: t.i32().primaryKey(),
idx: t.i64().index('btree').unique(),
};
type Row2 = InferTypeOfRow<typeof row2>;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type _ = MustBeNever<Row2>;

// Test type inference on a type with a nested object
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const point = t.object({
x: t.i32(),
y: t.f64(),
z: t.object({
foo: t.string(),
}),
});
type Point = InferTypeOfTypeBuilder<typeof point>;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _point: Point = {
x: 1.0,
y: 2.0,
z: {
foo: 'bar',
},
};

// Test type inference on an enum
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const e = t.enum({
A: t.string(),
B: t.number(),
});
type E = InferTypeOfTypeBuilder<typeof e>;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _e: E = { tag: 'A', value: 'hello' };
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _e2: E = { tag: 'B', value: 42 };

// Test that the type of a row includes the correct ColumnBuilder types
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _row3: {
foo: TypeBuilder<string, AlgebraicTypeVariants.String>;
bar: I32ColumnBuilder<{
isPrimaryKey: true;
isUnique: false;
isAutoIncrement: false;
isScheduleAt: false;
indexType: undefined;
}>;
idx: I64ColumnBuilder<{
isPrimaryKey: false;
isUnique: true;
isAutoIncrement: false;
isScheduleAt: false;
indexType: 'btree';
}>;
} = {
foo: t.string(),
bar: t.i32().primaryKey(),
idx: t.i64().index('btree').unique(),
};
Loading
Loading