是否可以编写一个接受字符串常量作为其参数之一并将其用作对象键的接口?
例如,假设我发出了两个不同的GraphQL请求,两个请求都返回User
,但是使用不同的键名:
const userByIdResult = {
data: {
userById: {
id: 123,
username: 'joseph'
}
}
}
const userByUsernameResult = {
data: {
userByUsername: {
id: 123,
username: 'joseph'
}
}
}
我想写一个通用接口会像这样:
interface GraphQLResponse<QueryKey, ResponseType> {
data: {
[QueryKey]: ResponseType
}
}
interface User {
username: string
id: string
}
type UserByIdResponse = GraphQLResponse<'userById', User>
type UserByUsernameResponse = GraphQLResponse<'userByUsername', User>
答案 0 :(得分:2)
您已经关闭。这属于category of Mapped Types下。您需要进行两项更改:
QueryKey extends string
key in QueryKey
interface GraphQLResponse<QueryKey extends string, ResponseType> {
data: {
[key in QueryKey]: ResponseType;
}
}
interface User {
username: string;
id: number;
}
type UserByIdResponse = GraphQLResponse<'userById', User>;
type UserByUsernameResponse = GraphQLResponse<'userByUsername', User>;
const userByIdResult: UserByIdResponse = {
data: {
userById: {
id: 123,
username: 'joseph'
}
}
}
const userByUsernameResult: UserByUsernameResponse = {
data: {
userByUsername: {
id: 123,
username: 'joseph'
}
}
}
const userByIdResultBoom: UserByIdResponse = {
data: {
userByUsername: {
id: 123,
username: 'joseph'
}
}
}