Skip to content

Infer typescript type from Schema #214

@Antoine-Regembal

Description

@Antoine-Regembal

Hi, thanks for the awesome package,

I could not find a way to infer a typescript type from a schema, that I could use elsewhere in my app to help build the correct object to insert in my repository.

For example considering this schema:

export const messageSchema = new Schema("message", {
    name: { type: "text" },
    category: { type: "string" },
    lastUpdated: { type: "date" },
    lastUpdater: { type: "string" },
});

I could totally write the typescript type myself and export it like:

export type MessageSchema = {
    name: string,
    category: string,
    lastUpdated: date,
    lastUpdater: string,
};

But if I update the schema I have to update the type.

Could we find a way to infer the typescript type from a Schema so its always sync with something like:

export const messageSchema = new Schema("message", {
    name: { type: "text" },
    category: { type: "string" },
    lastUpdated: { type: "date" },
    lastUpdater: { type: "string" },
});

export type MessageSchema = Schema.infer<typeof messageSchema> // {name: string; category: string; lastUpdated: string; lastUpdater: string}

This is inspired from the Zod type inference https://zod.dev/?id=type-inference

Thanks :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions