sample.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Add additional d.ts files to the JavaScript language service and change.
  2. // Also change the default compilation options.
  3. // The sample below shows how a class Facts is declared and introduced
  4. // to the system and how the compiler is told to use ES6 (target=2).
  5. // validation settings
  6. monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
  7. noSemanticValidation: true,
  8. noSyntaxValidation: false
  9. });
  10. // compiler options
  11. monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
  12. target: monaco.languages.typescript.ScriptTarget.ES6,
  13. allowNonTsExtensions: true
  14. });
  15. // extra libraries
  16. var libSource = [
  17. 'declare class Facts {',
  18. ' /**',
  19. ' * Returns the next fact',
  20. ' */',
  21. ' static next():string',
  22. '}',
  23. ].join('\n');
  24. var libUri = 'ts:filename/facts.d.ts';
  25. monaco.languages.typescript.javascriptDefaults.addExtraLib(libSource, libUri);
  26. // When resolving definitions and references, the editor will try to use created models.
  27. // Creating a model for the library allows "peek definition/references" commands to work with the library.
  28. monaco.editor.createModel(libSource, 'typescript', monaco.Uri.parse(libUri));
  29. var jsCode = [
  30. '"use strict";',
  31. '',
  32. 'class Chuck {',
  33. ' greet() {',
  34. ' return Facts.next();',
  35. ' }',
  36. '}'
  37. ].join('\n');
  38. monaco.editor.create(document.getElementById('container'), {
  39. value: jsCode,
  40. language: 'javascript'
  41. });