我试图弄清楚如何访问NPM模块中定义的类型。
TinyMCE有一个名为Editor
的编辑器React组件,它包含一个onInit
方法,它接受两个参数,一个事件和编辑器本身,也是一个Editor
类型,但与组件不同。
因此,我从库中导入Editor
组件
import { Editor as TinyMCEEditor } from '@tinymce/tinymce-react';
我定义编辑如下..。
<TinyMCEEditor
apiKey="asdf"
onInit={(evt, editor) => onInitCallBack(evt, editor)}
...
/>
然后,我尝试定义回调以匹配预期的类型。
const onInitCallBack = (_, editor: Editor) => {
...
};
奇怪的是,它能够识别Editor
类型,当它被鼠标化时,就给出了如下所示。
(parameter) editor: Editor
以及事件的类型:
(parameter) evt: {
readonly type: string;
readonly target: any;
readonly isDefaultPrevented: () => boolean;
readonly preventDefault: () => void;
...etc
}
但是在onInitCallBack
上,当我试图定义Editor
类型时,它说它找不到“编辑器”的名称:
type Editor = /*unresolved*/ any
Cannot find name 'Editor'. ts(2304)
要让它看到在NPM包中定义的Editor
类型,我需要做什么?
如果我尝试使用TinyMCEEditor
作为类型,我会得到以下错误。
(parameter) editor: Editor
Argument of type ‘import("e:/Git/web-app-release-notes-editor/node_modules/tinymce/tinymce").Editor’ is not assignable to
parameter of type ‘import("e:/Git/web-app-release-notes-editor/node_modules/@tinymce/tinymce-
react/1lib/cjs/main/ts/components/Editor").Editor’
Type ‘Editor’ is missing the following properties from type ‘Editor’: elementRef, boundHandlers, rollbackTimer, valueCursor, and
23 more. t5(2345)
发布于 2022-10-11 20:04:45
您混淆了React组件编辑器和tinymce编辑器--注意它们的名称相同。你需要这样的东西。
import { Editor } from '@tinymce/tinymce-react';
import { Editor as TinyMCEEditor } from 'tinymce';
然后
const editorRef = useRef<TinyMCEEditor | null>(null);
还有..。
<Editor
onInit={(evt, editor) => editorRef.current = editor}
...
/>
虽然我可能搞砸了这里的语法..。
https://stackoverflow.com/questions/74033277
复制相似问题