Typescript question

I have code like this but I think my types are wrong because I cant access shape of zod schema on my class, also on comments I have some questions, any idea? I would like to be able to get schema on my database class correctly:
import z, { type Schema } from "zod";

export class Database<T> {
public schema;
constructor({ schema }: { schema: Schema<T> }) {
this.schema = schema;
//here i cant this.schema.shape.user;
//console.log(this.schema.shape.user)

//how to access user object from schema
//this.schema.user
}

async create() {}
}
import z, { type Schema } from "zod";

export class Database<T> {
public schema;
constructor({ schema }: { schema: Schema<T> }) {
this.schema = schema;
//here i cant this.schema.shape.user;
//console.log(this.schema.shape.user)

//how to access user object from schema
//this.schema.user
}

async create() {}
}
import { it } from "vitest";
import { z } from "zod";
import { Database } from "./index";

const userSchema = z.object({
user: z.object({ id: z.number(), name: z.string() }),
});

//here i can userSchema.shape.user;

export type UserSchema = z.infer<typeof userSchema>;

it("Should create a database instance with correct schema", async () => {
const database = new Database<UserSchema>({ schema: userSchema });
//what should i do to be able to do database.user.create()
});
import { it } from "vitest";
import { z } from "zod";
import { Database } from "./index";

const userSchema = z.object({
user: z.object({ id: z.number(), name: z.string() }),
});

//here i can userSchema.shape.user;

export type UserSchema = z.infer<typeof userSchema>;

it("Should create a database instance with correct schema", async () => {
const database = new Database<UserSchema>({ schema: userSchema });
//what should i do to be able to do database.user.create()
});
7 Replies
Neto
Neto2y ago
just tested using zod and worked fine
Neto
Neto2y ago
rocawear
rocawearOP2y ago
Yes I know it works but its not correct
Neto
Neto2y ago
you want the shape to be the zod one or the "parsed" user one ?
rocawear
rocawearOP2y ago
I want to get user out of it
Neto
Neto2y ago
i think i get what you want
Neto
Neto2y ago
something like this?
Want results from more Discord servers?
Add your server