最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can I Wrap `ts-morph` Functions in Promises for TypeScript Compatibility in Node.js? - Stack Overflow

programmeradmin0浏览0评论

I'm using ts-morph in a Node.js environment, but its synchronous functions are blocking the main thread, which causes issues with a terminal spinner (since the spinner also runs on the main thread in the terminal).

import { Project, StructureKind } from "ts-morph";

// Initialize
const project = new Project({...});

const myEnumFile = project.createSourceFile("src/MyEnum.ts", {
    statements: [{
        kind: StructureKind.Enum,
        name: "MyEnum",
        isExported: true,
        members: [{ name: "member" }],
    }],
});

// Get information
const myClass = myClassFile.getClassOrThrow("MyClass");
myClass.getName();          // returns: "MyClass"
myClass.hasExportKeyword(); // returns: true
myClass.isDefaultExport();  // returns: false

How can I wrap all ts-morph functions in promises to avoid blocking the main thread, preferably without manually wrapping each one?

For example:

const myClass = myClassFile.getClassOrThrow("MyClass");

to:

const myClass = myClassFile.getClassOrThrowAsync("MyClass"); // returns a Promise

This should be done without losing the typing of the function.

发布评论

评论列表(0)

  1. 暂无评论