TypeScript导出/导入功能

Zed*_*ade 0 node.js node-modules typescript

我有一个我写的模块(通过私人仓库的npm安装),结构如下:

email.ts,utils.ts,index.ts,(其他一些文件)

在我的email.ts上我有以下功能:

export default function sendEmail(toEmail: string, subject: string, content: string): any {

    let helper = SendGrid.mail;

    let from_email: any = new helper.Email(process.env.FROM_EMAIL);
    let to_email: any = new helper.Email(toEmail);
    let helperContent: any = new helper.Content("text/plain", content);
    let mail: any = new helper.Mail(from_email, subject, to_email, helperContent)

    var request = SendGrid.emptyRequest({
        method: "POST",
        path: "/v3/mail/send",
        body: mail.toJSON()
    });

    //This performs the request with a promise
    SendGrid.API(request).then((response: any) => {
        //Deal with output as needed
        console.log("email was sent!!");
    }).catch((err: any) => {
        //log.error(err);
    });
}
Run Code Online (Sandbox Code Playgroud)

然后,在我的index.ts上,我有以下声明:

export * from "./email";
export * from "./errors";
export * from "./logger";
export * from "./objectUtils";
export * from "./queryFilter";
export * from "./templateEngine";
export * from "./utils";
Run Code Online (Sandbox Code Playgroud)

在我导入此模块的应用程序中,我将其导入为

import * as Utils from "my-utils";
Run Code Online (Sandbox Code Playgroud)

最后,每当我想使用sendEmail函数时,我都会使用以下语句来调用它:

Utils.sendEmail(email, subject, content);
Run Code Online (Sandbox Code Playgroud)

但是,这总是会引发错误"Cannot read property 'sendEmail' of undefined".

为什么会这样?导出这种方式时,我不能使用这种类型的声明吗?这是什么解决方案?

最好的祝福

tar*_*ing 5

尝试改变

export default function sendEmail
Run Code Online (Sandbox Code Playgroud)

只是

export function sendEmail
Run Code Online (Sandbox Code Playgroud)