I have created my own limited and simple version, it can infer the concrete typescript types from a schema, and it seems to work okay but before continuing further, I thought I would share it to see if a solution already exists, or if anyone can offer feedback.
type NumberTypes = NumberConstructor | number | 'port' | 'int' | 'nat' | 'duration' | 'timestamp';
type BooleanTypes = BooleanConstructor | boolean;
type UnknownTypes = '*';
type StringTypes = StringConstructor | string | 'url' | 'email' | 'ipaddress' | 'nat';
type InferredProps<T> = {
[k in keyof T]-?: T[k] extends SchemaObj
? T[k]['format'] extends NumberTypes
? number
: T[k]['format'] extends BooleanTypes
? boolean
: T[k]['format'] extends UnknownTypes
? unknown
: T[k]['format'] extends StringTypes
? string
: // eslint-disable-next-line @typescript-eslint/ban-types
T[k]['format'] extends Function
? // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
ReturnType<T[k]['format']>
: T[k]
: InferredProps<T[k]>;
// eslint-disable-next-line @typescript-eslint/ban-types
} & {};
Is there a method of inferring typescript types of getProperties?
Similar to @sinclair/typebox's Static and zod's infer
I have created my own limited and simple version, it can infer the concrete typescript types from a schema, and it seems to work okay but before continuing further, I thought I would share it to see if a solution already exists, or if anyone can offer feedback.
Given a schema like:
It can be used like
While using getConfig returns the correct keys, it fails to infer the type and just returns null
Any feedback welcome!