Monaco-editor: Trying to use `export` d.ts typings with monaco

Created on 28 Nov 2016  路  2Comments  路  Source: microsoft/monaco-editor

Hello, I am trying to use monaco in a browser. I want to add a number of existing .d.ts files (angular2 actually) to the project.

What I've tried was:

 const dog = `        
export class Dog {
  public bark(): string;
}`;
monaco.languages.typescript.typescriptDefaults.addExtraLib(dog, 'dog.d.ts');            

When I go to the editor, and try to import {Dog} from 'dog'; the dog module is not found.

Using module types worked for me

const dog = `       
declare module 'dog' {
  export class Dog {
    public bark(): string;
  }
}`;
monaco.languages.typescript.typescriptDefaults.addExtraLib(dog, 'dog.d.ts');            

When I go to the editor, and try to import {Dog} from 'dog'; it works.

Thanks!

monaco-typescript needs-more-info

Most helpful comment

You could prefix your typing definition key with the special node_modules/@types and set the typeRoots compiler option:

monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
    target: monaco.languages.typescript.ScriptTarget.ES2016,
    allowNonTsExtensions: true,
    moduleResolution: monaco.languages.typescript.ModuleResolutionKind.NodeJs,
    module: monaco.languages.typescript.ModuleKind.CommonJS,
    noEmit: true,
    typeRoots: [ "node_modules/@types" ]
});

monaco.languages.typescript.typescriptDefaults.addExtraLib(
    `export class Dog {
        public bark(): string;
    }`,
    "node_modules/@types/dog.d.ts");

monaco.editor.create(document.getElementById("container"), {
    model: monaco.editor.createModel(
        `import {Dog} from "dog";
console.log(new Dog().bark());`,
        "typescript",
        new monaco.Uri("main.ts")
    )
});

All 2 comments

You could prefix your typing definition key with the special node_modules/@types and set the typeRoots compiler option:

monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
    target: monaco.languages.typescript.ScriptTarget.ES2016,
    allowNonTsExtensions: true,
    moduleResolution: monaco.languages.typescript.ModuleResolutionKind.NodeJs,
    module: monaco.languages.typescript.ModuleKind.CommonJS,
    noEmit: true,
    typeRoots: [ "node_modules/@types" ]
});

monaco.languages.typescript.typescriptDefaults.addExtraLib(
    `export class Dog {
        public bark(): string;
    }`,
    "node_modules/@types/dog.d.ts");

monaco.editor.create(document.getElementById("container"), {
    model: monaco.editor.createModel(
        `import {Dog} from "dog";
console.log(new Dog().bark());`,
        "typescript",
        new monaco.Uri("main.ts")
    )
});

Yeah, what is the issue here? Is this a question about TypeScript?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Kang-Jun-sik picture Kang-Jun-sik  路  3Comments

poloten4uk picture poloten4uk  路  3Comments

brandalorian picture brandalorian  路  3Comments

akosyakov picture akosyakov  路  3Comments

PinkyJie picture PinkyJie  路  3Comments