|
1 | 1 | import { v } from ".."; |
2 | 2 | import { createRandomStringGenerator } from "../helpers/random"; |
3 | | -import { type TypeDefination, withAttrs } from "../schema"; |
| 3 | +import { |
| 4 | + type DefineOutput, |
| 5 | + type InferArgs, |
| 6 | + type TypeDefination, |
| 7 | + withAttrs, |
| 8 | +} from "../schema"; |
4 | 9 | import type { LiteralString } from "../types"; |
| 10 | +import type { VarDefination } from "../var"; |
| 11 | + |
| 12 | +/** Named stand-in for `v.var(name, { default: null, schema: v.object(shape) })`. |
| 13 | + * Inferring that return from `v.var` exceeds TS7056 on declaration emit. */ |
| 14 | +type ModelVar<N extends LiteralString, S> = VarDefination< |
| 15 | + N, |
| 16 | + DefineOutput<S> | null, |
| 17 | + TypeDefination<InferArgs<S>, DefineOutput<S>, never> & { shape: S } |
| 18 | +>; |
5 | 19 |
|
6 | 20 | export const generateId = v.fn( |
7 | 21 | "db.generate_id", |
@@ -49,8 +63,11 @@ export const id = <T, O>( |
49 | 63 |
|
50 | 64 | /** A model var: `v.var(name, { default: null, schema: v.object(shape) })`. |
51 | 65 | * Import it from the db plugin: `import { schema } from "better-call/plugins/db"`. */ |
52 | | -export const schema = <N extends LiteralString, S>(name: N, shape: S) => |
53 | | - v.var(name, { default: null, schema: v.object(shape) }); |
| 66 | +export const schema = <N extends LiteralString, S>( |
| 67 | + name: N, |
| 68 | + shape: S, |
| 69 | +): ModelVar<N, S> => |
| 70 | + v.var(name, { default: null, schema: v.object(shape) }) as ModelVar<N, S>; |
54 | 71 |
|
55 | 72 | export const db = { |
56 | 73 | unique, |
|
0 commit comments