Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer typescript types of getProperties #415

Open
cjsewell opened this issue May 25, 2023 · 0 comments
Open

Infer typescript types of getProperties #415

cjsewell opened this issue May 25, 2023 · 0 comments

Comments

@cjsewell
Copy link

cjsewell commented May 25, 2023

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.

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
} & {};

Given a schema like:

const testSchema = {
    number: {
        format: Number,
        default: null,
        env: 'SOME_VAR',
    },
    port: {
        format: 'port' as const,
        default: null,
        env: 'SOME_VAR',
    },
    duration: {
        format: 'duration' as const,
        default: null,
        env: 'SOME_VAR',
    },
    url: {
        format: 'url' as const,
        default: null,
        env: 'SOME_VAR',
    },
    string: {
        format: String,
        default: null,
        env: 'SOME_VAR',
    },
    bool: {
        format: Boolean,
        default: null,
        env: 'SOME_VAR',
    },
    nested: {
        string: {
            format: String,
            default: null,
            env: 'SOME_VAR',
        },
        bool: {
            format: Boolean,
            default: null,
            env: 'SOME_VAR',
        },
    },
};

It can be used like

type ConfigProps = InferredProps<typeof testSchema>;
const config = convict(testSchema);

const configPropsInferred = config.getProperties() as ConfigProps;
//    ^? const config: {number: number, port: number, duration: number, url: string, string: string, bool: boolean, nested: {string: string, bool: boolean}}

While using getConfig returns the correct keys, it fails to infer the type and just returns null

const configProps = config.getProperties();
//    ^? const configProps: {number: null, port: null, duration: null, url: null, string: null, bool: null, nested: {string: null, bool: null}}

Any feedback welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant