I imported import @mailchimp/mailchim_marketing in my NodeJS app:
import mailchimp from "@mailchimp/mailchimp_marketing";
However, it gives following error:
type.d.ts is not a module
I have searched to see if there is a @types/@mailchimp/mailchimp_marketing but I couldn't see it.
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.
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
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/ ).
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)
}
replace your code to this:
const mailchimp = require("@mailchimp/mailchimp_marketing");
Right, you wont have type safe but at least your code will work.
Types for @mailchimp/mailchimp_marketing
are available meanwhile.
Useyarn add -D @types/mailchimp__mailchimp_marketing
ornpm install --save-dev @types/mailchimp__mailchimp_marketing
to install the types package.
EDIT: the types do not seem to be complete.
With a similar issue with @mailchimp/mailchimp_transactional
I had to create my own mailchimp__mailchimp_transactional.d.ts
with declare module
(this package also has just types.d.ts
and it is almost empty unlike the types.d.ts
in mailchimp_marketing
package).
So you can type to create your own type description file using their types.d.ts
, place it in @types
folder of your project and add @types
to tsconfig.json
like this:
"compilerOptions": {
// other options here
"typeRoots": [
"@types",
"node_modules/@types"
]
mailchimp__mailchimp_transactional.d.ts
/* eslint-disable camelcase */
declare module '@mailchimp/mailchimp_transactional' {
...
}
一个快速而肮脏的解决方案是删除 types.d.ts 文件,这可以防止错误,尽管您将不再获得 API 的任何类型信息。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.