【发布时间】:2021-05-26 01:00:35
【问题描述】:
我想接受一个组件作为道具。
import AComponent from './AComponent.svelte';
type MyType = {
[key: string]: any; // just an example
}
type IWantToAcceptAComponent<T> = {
component: SvelteComponentTyped<{ record: T }>;
}
const componentPropObject: IWantToAcceptAComponent<MyType> = {
component: AComponent // error here
}
Error: Type 'typeof AComponent__SvelteComponent_' is missing the following properties from type
'SvelteComponentTyped<{ record: MyType; }>': $set, $on, $destroy, $$prop_def, and 5
more.ts(2740)
我认为 typedef 应该是:
type IWantToAcceptAComponent<T> = {
component: typeof SvelteComponentTyped<{ record: T }>;
}
但这会导致更奇怪的错误。
这种类型是显式的,这一点相当重要,因为它应该是用户导出界面的一部分。
【问题讨论】:
-
据我所知,将组件作为道具传递并不是一个好主意
-
为什么要将组件作为道具传递
-
主要是因为该组件非常基于数据,我可以保持所有相关类型的唯一方法,嗯,相关的,是通过我传递的道具的类型来管理这种关系。此外,说将组件作为道具(或至少是对象的一部分)传递是一个坏主意是一种谬论......它确实在 Svelte 快速入门中! svelte.dev/tutorial/svelte-component
标签: typescript svelte typescript-generics