简体   繁体   English

如何在导出功能中导入类?

[英]How to import class in export function?

I am trying to import class and use its public methods but its not working , what is correct way implement it. 我正在尝试导入类并使用其公共方法,但是它不起作用,实现它的正确方法是什么。

main.ts 主要

import {PromiseHandler } from './promiseHandler.ts';
    export function getUser(req: Request, res: Response) {
        const promiseHandler: new PromiseHandler();

    }

promiseHandler.ts promiseHandler.ts

export class PromiseHandler {

    constructor() {};
    public executeAllPromises(promises) {

}

As told in comments, the correct syntax is 如评论中所述,正确的语法是

 const promiseHandler = new PromiseHandler();

(note the use of = , to assign the object created, while : would be to type the variable. The type is actually just PromiseHandler, so you could use both and write : (请注意,使用=来分配创建的对象,而:是键入变量。该类型实际上只是PromiseHandler,因此可以同时使用和编写:

const promiseHandler: PromiseHandler = new PromiseHandler(); 

but I think that is not necessary to declare type here, TypeScript would detect it the correct type by itself with the initialisation with = new... 但我认为不必在这里声明类型,TypeScript可以通过= new...初始化本身来检测出正确的类型= new...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM