creating a type based on the input

export function getType(cols: Column[] | undefined){
if (!cols) return z.object({});
const schema = {};
cols.forEach((col) => {
schema[col.id] = z.string();
});
return z.object(schema);
}
export function getType(cols: Column[] | undefined){
if (!cols) return z.object({});
const schema = {};
cols.forEach((col) => {
schema[col.id] = z.string();
});
return z.object(schema);
}
im getting this error on schema[col.id]
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
No index signature with a parameter of type 'string' was found on type '{}'.
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
No index signature with a parameter of type 'string' was found on type '{}'.
1 Reply
prakhar_goel
prakhar_goel14mo ago
You need to use the setKey method. const schema = someData.reduce( (schema, definition) => schema.setKey(definition.name, z.string()), z.object({}) );