Closed Qrokqt closed 1 month ago
@Qrokqt Hi, try the following...
import { Type, TObject, TString } from '@sinclair/typebox'
type Allowed = TObject<{ [key: string]: TString }>
function doSomething<T extends Allowed>(schema: T) { /* ... */ }
//good
doSomething(Type.Object({}));
doSomething(Type.Object({
foo: Type.String(),
bar: Type.String(),
}));
//bad
doSomething(Type.Number());
doSomething(Type.Object({
foo: Type.Number(),
});
doSomething(Type.Object({
foo: Type.Object({
bar: Type.String(),
}),
}));
Hope this helps S
Perfect, thanks!
Unfortunately this is not a general solution. I'm trying to define a SchemaFor<T>
but even the simplest attempt runs into a "type instantiation is excessively deep and possibly infinite" error.
This is what I tried:
type SchemaFor<T> = T extends string ? ReturnType<typeof Type.String>
: T extends number ? ReturnType<typeof Type.Number>
: T extends boolean ? ReturnType<typeof Type.Boolean>
: T extends object ? TObject<{ [P in keyof T]: SchemaFor<T[P]> }>
: TSchema;
I'm looking for a way to pass a schema to a function and have typescript throw an error if the schema doesnt conform to a type, is this possible?