[英]How to dynamically assign a name and type to unknown object property in Typescript
I'm using Shopify's rest client for node and its request and response look something like this:我正在使用 Shopify 的 rest 客户端作为节点,它的请求和响应如下所示:
client.get({
path: 'orders/count.json',
query: { fulfillment_status: 'unfulfilled' }
})
{
"errors": "[API] Invalid API key or access...",
"code": 2342,
"statusText": "Authentication Error",
"Headers": "..."
}
{
"body": { "count": 8 },
"code": 2342,
"statusText": "Authentication Error",
"Headers": "..."
}
I'd like to add some boilerplate over this client library so that I can get the typings of the response.我想在这个客户端库上添加一些样板,以便我可以获得响应的类型。 This is what I'm trying to do but it's not working too well:
这是我正在尝试做的,但效果不太好:
const customClient = {
get: async <T, K extends string>(params: GetRequestParams) => {
const response = (await client.get(params));
if (response.body.errors) return { errors: response.body.errors };
// somehow index it. obviously not with the type declaration???
return { [K]: response.body[K] as T };
},
}
With the hopes that I can use it as.希望我可以将其用作。
const { count, error } = customClient.get<number, "count">({ ... });
Any help would be appreciated.任何帮助,将不胜感激。 I have an entire file of the Shopify API types that I would like to leverage.
我有一个我想利用的 Shopify API 类型的完整文件。 A solution to this would be perfect
解决这个问题将是完美的
A possible implementation can look like this:一个可能的实现如下所示:
const customClient = {
get: async <T, K extends string>(params: GetRequestParams):
Promise<Partial<Record<K, T> & { errors: string }>> =>
{
const response = (await client.get(params));
if (response.body.errors) return { errors: response.body.errors } as any;
return {
[Object.keys(response)[0]]: response[Object.keys(response)[0]]
} as any
},
}
As you correctly noted, we can't use the TypeScript generic types when constructing the returning object.正如您正确指出的那样,在构造返回的 object 时,我们不能使用 TypeScript 泛型类型。 We need to use JavaScript features instead.
我们需要改用 JavaScript 功能。 In this case I just took the first key of the
response
and used it for the key of the returning object as well as to index the response
object.在这种情况下,我只取
response
的第一个键并将其用作返回的 object 的键以及索引response
object。
The return type of the function is a Promise
consisting of both a Record
with K
and T
as keys and values and the error type. function 的返回类型是一个
Promise
,它由以K
和T
作为键和值的Record
和错误类型组成。 I used Partial
here since they are not both required.我在这里使用了
Partial
,因为它们都不是必需的。
Destructing the response leads to the correct types now:现在破坏响应会导致正确的类型:
async function main(){
const { count, errors } = await customClient.get<number, "count">({} as any);
// count: number | undefined
// errors: string | undefined
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.