我有:
import sendgridClient from '@sendgrid/client'
sendgridClient.setApiKey(process.env.SENDGRID_API_KEY);
const sendgridRequest = {
method: 'PUT',
url: '/v3/marketing/contacts',
body: {
list_ids: [myId],
contacts: [
{
email: req.body.email,
custom_fields: {
[myFieldId]: 'in_free_trial'
}
}
]
}
};
await sendgridClient.request(sendgridRequest);
但是我的 TypeScript 语言服务器给了我一个关于 sendgridRequest
的错误。 :Argument of type '{ method: string; url: string; body: { list_ids: string[]; contacts: { email: any; custom_fields: { e5_T: string; }; }[]; }; }' is not assignable to parameter of type 'ClientRequest'.
Types of property 'method' are incompatible.
Type 'string' is not assignable to type 'HttpMethod'.
有没有办法解决这个问题?
最佳答案
在没有原始类型的情况下执行此操作的另一种方法:
method: 'PUT' as const,
字符串不能分配给 HttpMethod,但字符串文字 'PUT' 是!更多详情:https://stackoverflow.com/a/66993654/91713
关于javascript - SendGrid 客户端 TypeScript 错误 : HttpMethod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67812159/