TypeScript Function That Generates a Schema with Dynamic Fields and Maintains Correct Return Types

I’m trying to create a TypeScript function that takes configuration parameters and generates a schema with some default fields. However, I’m struggling to get the correct return type for the function. Specifically, the autocomplete doesn’t show the fields created from the parameters. Here’s an example of what I’m trying to achieve:
function createSchema(tableName: string, fields: {
name: string;
type: "text" | "integer" | ...
required?: boolean;
unique?: boolean;
}[]) {
return mysqlTable(tableName, {
id: int().autoincrement().primaryKey(),
createdAt: timestamp({ mode: "date" }).defaultNow(),
updatedAt: timestamp({ mode: "date" }).defaultNow(),
...fields.reduce((acc, field) => {
return { ...acc, ...resolveField(field) };
}, {})
});
}
function createSchema(tableName: string, fields: {
name: string;
type: "text" | "integer" | ...
required?: boolean;
unique?: boolean;
}[]) {
return mysqlTable(tableName, {
id: int().autoincrement().primaryKey(),
createdAt: timestamp({ mode: "date" }).defaultNow(),
updatedAt: timestamp({ mode: "date" }).defaultNow(),
...fields.reduce((acc, field) => {
return { ...acc, ...resolveField(field) };
}, {})
});
}
Any suggestions or examples would be greatly appreciated! Thanks in advance!
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?