monaco-editor-auto-typings

View the demo at lukasbach.github.io/monaco-editor-auto-typings
monaco-editor-auto-typings is a plugin for Microsoft's Monaco Editor,
which automatically loads type declarations when you enter import-calls in the code editor.
Try it out in the demo! Add some imports to some
libraries available on npm, such as import express from 'express';
, and see that any imported variables
are automatically strictly typed.
monaco-editor-auto-typings comes with lots of customization options, but is still a one-liner to
add to your project. It works by loading declarations from UnPkg. They can then be optionally
cached in localStorage or elsewhere.

Example
Install via yarn add monaco-editor-auto-typings monaco-editor
or npm install monaco-editor-auto-typings monaco-editor --save
.
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
import { AutoTypings, LocalStorageCache } from 'monaco-editor-auto-typings';
const val = `
import React from 'react';
React.useEffect(0); // Type Error!
`;
const editor = monaco.editor.create(document.getElementById('root')!, {
model: monaco.editor.createModel(val, 'typescript'),
});
const autoTypings = await AutoTypings.create(editor, {
sourceCache: new LocalStorageCache(),
});
Custom Monaco Version
By default, monaco-editor-auto-typings directly imports the monaco editor library itself. You can
customize the monaco object with the monaco
option. If you want to skip the entire import
of monaco when bringing your own instance of monaco, you can import from monaco-editor-auto-typings/custom-editor
instead of monaco-editor-auto-typings
.
Configuration
https://lukasbach.github.io/monaco-editor-auto-typings/docs/interfaces/Options.html
export interface Options {
shareCache: boolean;
onlySpecifiedPackages: boolean;
preloadPackages: boolean;
dontAdaptEditorOptions: boolean;
dontRefreshModelValueAfterResolvement: boolean;
versions?: { [packageName: string]: string };
onUpdateVersions?: (versions: { [packageName: string]: string }) => void;
sourceCache: SourceCache;
sourceResolver: SourceResolver;
fileRootPath: string;
debounceDuration: number;
packageRecursionDepth: number;
fileRecursionDepth: number;
onUpdate?: (update: ProgressUpdate, textual: string) => void;
onError?: (error: string) => void;
}