@ mailchimp/mailchimp_marketing/types.d.ts'不是nodeJs中的模块 [英] @mailchimp/mailchimp_marketing/types.d.ts' is not a module in nodeJs
问题描述
我在NodeJS应用中导入了导入@ mailchimp/mailchim_marketing:
I imported import @mailchimp/mailchim_marketing in my NodeJS app:
import mailchimp from "@mailchimp/mailchimp_marketing";
但是,它给出了以下错误:
However, it gives following error:
type.d.ts不是模块
type.d.ts is not a module
我已经搜索过是否有@ types/@ mailchimp/mailchimp_marketing,但看不到.
I have searched to see if there is a @types/@mailchimp/mailchimp_marketing but I couldn't see it.
推荐答案
@ mailchimp/mailchimp_marketing提供的 type.d.ts
文件没有库的类型,程序包没有也没有@types包.因此,必须创建自己的应用来覆盖他提供的内容.
The type.d.ts
file provided by @mailchimp/mailchimp_marketing doesn't have the types of the library and the package doesn't have a @types package too. So it's necessary to create your own to override the provided by him.
为此,请在以下文件夹中创建文件夹 @ types/@ mailchimp/mailchimp_marketing
(一个文件夹),并在 mailchimp_marketing内部创建文件
index.d.ts
.
To do this, creates the folders @types/@mailchimp/mailchimp_marketing
(one inside another) and creates the file index.d.ts
inside mailchimp_marketing
.
此文件必须包含模块的声明,并且必须包含要在库中使用的函数和类型.就我而言:
This file has to contain the declaration of the module, and inside than, the functions and types what you gonna use from library. In my case:
declare module '@mailchimp/mailchimp_marketing' {
type Config = {
apiKey?: string,
accessToken?: string,
server?: string
}
type SetListMemberOptions = {
skipMergeValidation: boolean
}
export type SetListMemberBody = {
email_address: string,
status_if_new: 'subscribed' | 'unsubscribed' | 'cleaned' | 'pending' | 'transactional'
merge_fields?: {[key: string]: any}
}
export default {
setConfig: (config: Config) => {},
lists: {
setListMember: (listId: string, subscriberHash: string, body: SetListMemberBody, opts?: SetListMemberOptions): Promise<void> => {}
}
}
}
SetListMemberBody
具有更多字段,并且 setListMember
并非无效,但我只添加了我将要使用的内容.为了发现这些字段和功能,我查看了源代码( https://github.com/mailchimp/mailchimp-marketing-node )和api文档( https://mailchimp.com/developer/api/marketing/list-members/add-or-update-list-member/).
SetListMemberBody
has much more fields and setListMember
is not void, but i added just what I gonna use. To discover this fields and functions I looked in the source code (https://github.com/mailchimp/mailchimp-marketing-node) and api documentation (https://mailchimp.com/developer/api/marketing/list-members/add-or-update-list-member/).
对于我来说(Typescript 3.7.3)不需要更改 tsconfig.json
,但是如果您使用旧版本,则可能需要添加"typeRoots".tsconfig.json中的您的compileOptions:
In my case (Typescript 3.7.3) was not necessary to change tsconfig.json
, but if you use older version maybe is necessary to add "typeRoots" for your compilerOptions in tsconfig.json:
"compilerOptions": {
"typeRoots": ["@types", "node_modules/@types"]`
// other options
}
毕竟,我通常使用该库:
After all this, I used the library normally:
import mailchimp, { SetListMemberBody } from '@mailchimp/mailchimp_marketing'
import crypto from 'crypto'
mailchimp.setConfig({
apiKey: process.env.MAILCHIMP_KEY,
server: 'serverHere',
});
const listId = 'listIdHere'
export const addSubscriber = async (member: SetListMemberBody): Promise<void> => {
const hash = crypto.createHash('md5').update(member.email_address).digest('hex')
await mailchimp.lists.setListMember(listId, hash, member)
}
这篇关于@ mailchimp/mailchimp_marketing/types.d.ts'不是nodeJs中的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!