admin管理员组文章数量:1323035
I have:
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);
But my TypeScript language server gives me an error about 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 inpatible.
Type 'string' is not assignable to type 'HttpMethod'.
Is there some way to resolve this?
I have:
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);
But my TypeScript language server gives me an error about 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 inpatible.
Type 'string' is not assignable to type 'HttpMethod'.
Is there some way to resolve this?
Share Improve this question asked Jun 2, 2021 at 20:30 ShamoonShamoon 43.6k101 gold badges329 silver badges628 bronze badges2 Answers
Reset to default 7Another way of doing this in the absence of the original types:
method: 'PUT' as const,
A string is not assignable to HttpMethod, but the string literal 'PUT' is!
More details: https://stackoverflow./a/66993654/91713
method: 'PUT'
in your object is being inferred as string
, but it's expecting specific strings like "PUT" | "GET" | "POST"
. This because it has no specific type to try to match, and by default specific strings are just inferred as string
.
You can probably fix this by passing your object directly to the function. This casts the object as the right type because it's checked against what that function accepts:
await sendgridClient.request({
method: 'PUT',
url: '/v3/marketing/contacts',
body: {
list_ids: [myId],
contacts: [
{
email: req.body.email,
custom_fields: {
[myFieldId]: 'in_free_trial'
}
}
]
}
})
Or you can give your intermediate variable the correct type imported from the sendgrid module.
import sendgridClient, { ClientRequest } from '@sendgrid/client'
const sendgridRequest: ClientRequest = { /* ... */ }
await sendgridClient.request(sendgridRequest);
I wasn't able to test this because this module doesn't seem import into the typescript playground but I think that should work.
本文标签: javascriptSendGrid client TypeScript Error HttpMethodStack Overflow
版权声明:本文标题:javascript - SendGrid client TypeScript Error: HttpMethod - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742109363a2421173.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论