Peter
Peter

Reputation: 13

Monaco Editor: Creating TypeScript-Declarations automatically

i am using the Monaco Editor and i wonder, if it is possible, to create typescript declarations (*.d.ts) out of typescript-codes.

Something like this pseudocode:

var myTsCode = "function greeter(person: string): string { return 'Hello, ' + person; }";
var myDTS = monaco.languages.typescript.createdts(myTsCode); // returns 'declare function greeter(person: string): string;'

monaco.languages.typescript.typescriptDefaults.addExtraLib(myDTS, "filename/my.d.ts");

(Basically the same way i can do via Commandline: "tsc myTsCode.ts -d")

Thanks in advance & Greetings ... Peter

Upvotes: 1

Views: 765

Answers (1)

AndersMad
AndersMad

Reputation: 338

Yes, apparently that is possible - try this (incomplete code):

let compilerOptions = (monaco.languages.typescript.typescriptDefaults as any)
    .getCompilerOptions(); // getCompilerOptions is not "public"
compilerOptions.declaration = true;
monaco.languages.typescript.typescriptDefaults.setCompilerOptions(compilerOptions);

Then

getWorker.then((worker: any) => {
    worker(model.uri)
        .then((client: any) => {
            client.getEmitOutput(model.uri.toString()).then((result: any) => {
                console.log(result); // result.outputFiles[1].text
            });
        });
});

The second file in the array is 1.d.ts

It can also generate a 1.js.map source map file by compilerOptions.sourceMap = true;

Upvotes: 3

Related Questions