dsherret / ts-morph

TypeScript Compiler API wrapper for static analysis and programmatic code changes.
https://ts-morph.com
MIT License
5.03k stars 196 forks source link

NEW API:SourceFile.addGlobalDeclare #1565

Open peterroe opened 3 months ago

peterroe commented 3 months ago

Can we add an API for generating declare modules?

My code now:

const sourceFile = project.createSourceFile(/*...*/)

// some code...

sourceFile.insertText(
        sourceFile.getEnd(),
        `
declare global {
    declare const __DEV__: boolean;
    declare const __EDITOR__: boolean;
    declare const PW: PW;

    interface Window {
        require: (name: string) => unknown;
        PW: PW;
    }
}
`
    );

My preferred usage:

const dm = sourceFile.addGlobalDeclare({
    // more options
})

If the idea is deemed viable, I would happily pull a request.