最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SendGrid client TypeScript Error: HttpMethod - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 7

Another 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.

发布评论

评论列表(0)

  1. 暂无评论