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 type from Schema #214

Closed
Antoine-Regembal opened this issue Oct 19, 2023 · 2 comments
Closed

Infer typescript type from Schema #214

Antoine-Regembal opened this issue Oct 19, 2023 · 2 comments

Comments

@Antoine-Regembal
Copy link

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 :)

@Didas-git
Copy link
Contributor

Didas-git commented Oct 27, 2023

Right now this is not built into redis-om.
You can however build your own type to do that mapping which is rather simple but would require wrapping the schema call.

Alternatively if you are interested you can take a look at #188

@Antoine-Regembal
Copy link
Author

I'll check that thanks

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

2 participants