Is there a method in TypeScript for defining a dynamic type that can be based on projection parameters?
For example, when executing a database query or GraphQL request, you might have a function like this:
interface Person {
id: string;
firstName: string;
lastName: string;
}
function getPeople(projection: (keyof Person)[]): Promise<Partial<Person>[]> {...}
If I call getPeople(['id', 'firstName'])
, the returned objects will not include lastName
since it was excluded from the projection. Is there a way to indicate this at the type level, rather than using Partial<Person>
which still allows access to uninitialized properties?