我有一个Monaco编辑器,用户输入了自定义的javascript代码。在此Monaco Editor中,他们可以使用Lodash功能。我希望能够通过包括类型定义为他们提供lodash的智能/代码补全。
我看到了一些与添加自定义声明有关的答案,但是没有一个答案包含完整的第三方库声明。有没有人有经验。
这是我到目前为止所拥有的。然后在下面创建编辑器,类似于文档中的示例。
monaco.languages.typescript.typescriptDefaults.addExtraLib("", "./../../types/lodash/index.d.ts");
答案 0 :(得分:2)
将其作为示例和api。您应该将.d.ts
文件的内容作为第一个参数传递
monaco.languages.typescript.typescriptDefaults.addExtraLib(content, "")
检查this演示如何传递参数
答案 1 :(得分:2)
我想添加包 monaco-editor-auto-typings 作为选项。免责声明:我是该软件包的开发者。
它不断扫描在 monaco 编辑器中输入的代码,检测导入并自动从 UnPkg 加载声明文件。
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!
`;
// Create monaco editor instance
const editor = monaco.editor.create(document.getElementById('root')!, {
model: monaco.editor.createModel(val, 'typescript'),
});
// Initialize auto typing on monaco editor. Imports will now automatically be typed!
const autoTypings = AutoTypings.create(editor, {
sourceCache: new LocalStorageCache(), // Cache loaded sources in localStorage. May be omitted
// Other options...
});
您可以在 demo 中探索它的工作原理。
答案 2 :(得分:0)
(从我的GH原理中复制:https://gist.github.com/cdrini/9de507f6ac19da30fd27c5f618783b31)
那真是令人头疼!这当然不是一个很好的解决方案,但它可以工作。希望有人可以使用这些笔记来节省大量时间。
已知问题:
list
和raw-loader
@types/lodash
(在撰写本文时,它们分别位于4.14.162和4.0.2)
查看@types/lodash/index.d.ts,复制所有npm install --save-dev @types/lodash raw-loader
引用并将其导入。 :
common
import LODASH_index from '!raw-loader!@types/lodash/index.d.ts';
import LODASH_common from '!raw-loader!@types/lodash/common/common.d.ts';
import LODASH_array from '!raw-loader!@types/lodash/common/array.d.ts';
import LODASH_collection from '!raw-loader!@types/lodash/common/collection.d.ts';
import LODASH_date from '!raw-loader!@types/lodash/common/date.d.ts';
import LODASH_function from '!raw-loader!@types/lodash/common/function.d.ts';
import LODASH_lang from '!raw-loader!@types/lodash/common/lang.d.ts';
import LODASH_math from '!raw-loader!@types/lodash/common/math.d.ts';
import LODASH_number from '!raw-loader!@types/lodash/common/number.d.ts';
import LODASH_object from '!raw-loader!@types/lodash/common/object.d.ts';
import LODASH_seq from '!raw-loader!@types/lodash/common/seq.d.ts';
import LODASH_string from '!raw-loader!@types/lodash/common/string.d.ts';
import LODASH_util from '!raw-loader!@types/lodash/common/util.d.ts';
文件,但不会出错。然后将它们注册到monaco中(无论项目中的monaco位于何处):
.d.ts
注意:
window.monaco?.languages.typescript.javascriptDefaults.addExtraLib(LODASH_index, '@types/lodash/index.d.ts');
window.monaco?.languages.typescript.javascriptDefaults.addExtraLib(LODASH_common, '@types/lodash/common/common.d.ts');
window.monaco?.languages.typescript.javascriptDefaults.addExtraLib(LODASH_array, '@types/lodash/common/array.d.ts');
window.monaco?.languages.typescript.javascriptDefaults.addExtraLib(LODASH_collection, '@types/lodash/common/collection.d.ts');
window.monaco?.languages.typescript.javascriptDefaults.addExtraLib(LODASH_date, '@types/lodash/common/date.d.ts');
window.monaco?.languages.typescript.javascriptDefaults.addExtraLib(LODASH_function, '@types/lodash/common/function.d.ts');
window.monaco?.languages.typescript.javascriptDefaults.addExtraLib(LODASH_lang, '@types/lodash/common/lang.d.ts');
window.monaco?.languages.typescript.javascriptDefaults.addExtraLib(LODASH_math, '@types/lodash/common/math.d.ts');
window.monaco?.languages.typescript.javascriptDefaults.addExtraLib(LODASH_number, '@types/lodash/common/number.d.ts');
window.monaco?.languages.typescript.javascriptDefaults.addExtraLib(LODASH_object, '@types/lodash/common/object.d.ts');
window.monaco?.languages.typescript.javascriptDefaults.addExtraLib(LODASH_seq, '@types/lodash/common/seq.d.ts');
window.monaco?.languages.typescript.javascriptDefaults.addExtraLib(LODASH_string, '@types/lodash/common/string.d.ts');
window.monaco?.languages.typescript.javascriptDefaults.addExtraLib(LODASH_util, '@types/lodash/common/util.d.ts');
扩展名会导致它们中断。.d.ts
:https://microsoft.github.io/monaco-editor/playground.html#extending-language-services-configure-javascript-defaults addExtraLib
:https://github.com/search?l=JavaScript&q=addExtraLib&type=Code的用途