interface User {
id: number;
username: string;
password: string;
email: string;
}
interface Order {
id: number;
user_id: number;
quantity: number;
}
interface Tables {
user: User;
order: Order;
}
// Expected type of responseColumnMap = {name: 'user.username', qty: 'order.quantity'}
type responseColumnMap = {
[key: string]: `${infer R}.${keyof Tables[R] & string}`
}
I am trying to create a map type using template literal syntax in which the values of the map will have type ${tableName}.${columnName}.
The columName should be constrained to allow only columns from their respective tables.
I tried inferring the table name and using that inferred type to create the type but I am getting infer' declarations are only permitted in the 'extends' clause of a conditional type. error.
How can I create the type that I am looking for?
I have attached the playground link with the minimal code.
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.