Is it possible to write an interface that accepts a string constant as one of its parameters, and uses that as the key of an object?
For instance, assuming I make two different GraphQL requests, both of which return a User, but under different key names:
const userByIdResult = {
data: {
userById: {
id: 123,
username: 'joseph'
}
}
}
const userByUsernameResult = {
data: {
userByUsername: {
id: 123,
username: 'joseph'
}
}
}
I would imagine writing a generic interface would go something like this:
interface GraphQLResponse<QueryKey, ResponseType> {
data: {
[QueryKey]: ResponseType
}
}
interface User {
username: string
id: string
}
type UserByIdResponse = GraphQLResponse<'userById', User>
type UserByUsernameResponse = GraphQLResponse<'userByUsername', User>
But, this doesn't work.