我正在开发一个像https://github.com/eclipse-theia/theia/blob/master/packages/plugin/src/theia.d.ts这样的插件系统。
我的@my-scope/plugin包提供类型定义。在加载插件时,插件上下文将像这样传递给插件启动函数:
// import React from 'react' // Should NOT import react in a plugin
import * as from '@my-scope/plugin';
export function start(ctx: my.PluginContext) {
// Dialog and Button component should have correct
const { Dialog, Button } = ctx.ui; types
ctx.registerDialog('dialog1', () => {
// onCustomEvent should be auto-suggested
return <Dialog onCustomEvent={...}><Button onClick={...}>OK</Button></Dialog>;
});
}(为了使JSX工作,我可以将jsxFactory设置为ctx.React.createElement in tsconfig.json作为解决办法。)
在iframe或web worker中,插件不是孤立的,而是与它的主机共享全局(因为遗留的原因)。该插件还需要使用库创建React组件。因此,我必须在插件上下文中公开React和UI库,因为应用程序中应该只有一个React,所有插件都共享公共UI库(以及其他库,如axios)。
我的问题是如何在插件上下文定义中重新导出React和UI库,以便插件能够访问正确的类型并在JSX中具有自动建议。
export module "@my-scope/plugin" {
export interface PluginContext {
React: ??? // how to re-export React types here
ui: ???; // how to re-export UI library types here
registerDialog(dialogName: string, () => JSX.Element);
}
}发布于 2022-07-17 01:50:44
幸运的是,当我浏览@rollup/plugin-typescript源代码时,我发现以下代码解决了我的问题:
typescript?: typeof import('typescript');在我的情况下
export module "@my-scope/plugin" {
export interface PluginContext {
React: typeof import('react');
ui: typeof import('some-ui-lib');
registerDialog(dialogName: string, () => JSX.Element);
}
}https://stackoverflow.com/questions/73001404
复制相似问题