在 TypeScript 中输入 <svelte:component> 的“this”属性

Jea*_*hel 5 typescript svelte

我正在 Svelte 中构建一种门户系统,使用商店传递组件以在组件树的某个位置显示。

我的问题不在于实施该系统,而在于键入商店。我试图这样声明:

const modalComponent = writable<SvelteComponent>(null)
Run Code Online (Sandbox Code Playgroud)

但这不起作用。当我在某处导入组件时,VS Code 向我显示了一种类型typeof <componentName>__SvelteComponent_,该SvelteComponent类型与该类型不兼容(实际上是 的别名SvelteComponentDev):

类型 'typeof <component>__SvelteComponent_' 缺少来自类型 'SvelteComponentDev' 的以下属性:$set、$on、$destroy、$capture_state 等。

我该怎么打这个字?我想避免使用any.

更新:这是一个可以重现该案例的代码沙盒。不幸的是,我似乎无法让它与 TypeScript 一起工作。我还在分享,希望能帮到你。

Jea*_*hel 11

编辑(2023)
现在已修复并且应该输入writable<typeof SvelteComponent>

(是的,很久以前就已经修复了,抱歉:D)


正如@dummdidumm 指示我的那样,我在这里为 Svelte 团队创建了一个问题来解决这个问题。

同时,我将暂时将此标记为答案,并在问题修复和发布后立即更新。谢谢!

  • 现在已经修好了。您还应该注意,答案中是“writable&lt;typeof SvelteComponent&gt;”,而不是“writable&lt;SvelteComponent&gt;”。 (12认同)