1

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

1 Answer 1

3

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;

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.