javascript - SendGrid 客户端 TypeScript 错误 : HttpMethod

标签 javascript node.js typescript sendgrid sendgrid-api-v3

我有:

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/

相关文章:

javascript - webRTC - 区分临时断开或故障与永久

javascript - 添加描边并更改数据圈中文本的文本颜色

javascript - 命令行 chrome 扩展加载和测试

node.js - ionic 2 构建类型错误 : Cannot read property 'AssetUrl' of undefined

javascript - 添加新项目时 vsCode 刷新树

angular - 使用一种方法将多个变量设置为 false Angular4

javascript - 对象定义中的方括号表示法

reactjs - 使用 React、Typescript 和 ES6 进行 Jest 测试

javascript - 更改 JavaScript 中的默认文件分辨率

json - Node.js fs错误扩展