I would like to create a type Projection<T> that describes an object whose properties can only be a number (0 or 1) or a Projection of the type of that attribute.
Example:
Given types:
interface Person {
name: string;
address: Address
}
interface Address {
street: string;
country: string;
}
The Projection<Person> would be:
const p: Projection<Person> = { // this is a Projection<Person>
name: 1,
address: { // this is a Projection<Address>
street: 0,
country: 1
}
}
Is that possible at all in typescript? I am getting stuck in the recursive Projection part
export type Projection<T> = { [K in keyof T]?: number | Projection<???> };
Using number | Projection<any> works, but of course does not check if the fields declared belong to Address