如何正确推断 Svelte 属性类型?

huangapple go评论101阅读模式
英文:

How to correctly infer Svelte props type?

问题

Option 1: Type-cast the $$props

<script lang="ts">
    interface ComponentPropType {
        a: string;
        b: number;
    }

    export let { a, b } = $$props as ComponentPropType;
</script>

<h1>a = {a} </h1>
<p>My value is {b} </p>

Option 2: De-structure the props specified by export let

<script lang="ts">
    interface ComponentPropType {
        a: string;
        b: number;
    }
    export let props: ComponentPropType;
    export let { a, b } = props;
</script>

<h1>a = {a} </h1>
<p>My value is {b} </p>

If we use Option 1 - we can render the component as <MyComponent {{a, b}} /> or <MyComponent {a} {b} />.

On the other hand, if we use Option 2 - we need to render the component as <MyComponent props={{a, b}} />. In this case, we can't use the prop-spread.

Which one is the correct way to do it? Is there a better solution?

英文:

I have a svelte component (SvelteKit Project) - let's call it MyComponent. It accepts two props.

What would be the correct way to do it with TypeScript?

Option 1: Type-cast the $$props

&lt;script lang=&quot;ts&quot;&gt;
    interface ComponentPropType {
        a: string;
        b: number;
    }

	export let { a, b } = $$props as ComponentPropType;
&lt;/script&gt;

&lt;h1&gt;a = {a} &lt;/h1&gt;
&lt;p&gt;My value is {b} &lt;/p&gt;

Or should I go with a bit verbose

Option 2: De-structure the props specified by export let

&lt;script lang=&quot;ts&quot;&gt;
    interface ComponentPropType {
        a: string;
        b: number;
    }
    export let props: ComponentPropType;
	export let { a, b } = props;
&lt;/script&gt;

&lt;h1&gt;a = {a} &lt;/h1&gt;
&lt;p&gt;My value is {b} &lt;/p&gt;


If we use Option 1 - we can render the component as &lt;MyComponent {{a, b}} /&gt; or &lt;MyComponent {a} {b} /&gt;
Does this one prevent event forwarding (if any)?

On the other hand, if we use Option 2 - we need to render the component as &lt;MyComponent props={{a, b}} /&gt;.
In this case, we can't use the prop-spread.

Which one is the correct way to do it? Is there a better solution?

答案1

得分: 2

我不认为那是必要的,但我并不完全确定你想要实现什么。

**应该**足够的是只需用`export let`定义props

```html
&lt;script lang=&quot;ts&quot;&gt;
    export let a: string
    export let b: number;
&lt;/script&gt;

&lt;h1&gt;a = {a} &lt;/h1&gt;
&lt;p&gt;我的值是 {b} &lt;/p&gt;

然后你可以使用直接赋值

&lt;Component a={&quot;a&quot;} b={2} /&gt;

或者扩展

&lt;Component {...{a:&quot;a&quot;, b:2}} /&gt;

请注意需要使用扩展运算符,因为{{a,b}}{...{a,b}}不是相同的。

英文:

I don't think that's necessary, but I'm not 100% sure what you are trying to achieve.

What should be sufficient is to just define the props with export let

&lt;script lang=&quot;ts&quot;&gt;
    export let a: string
    export let b: number;
&lt;/script&gt;

&lt;h1&gt;a = {a} &lt;/h1&gt;
&lt;p&gt;My value is {b} &lt;/p&gt;

This will then be enough for you to use either direct assignment

&lt;Component a={&quot;a&quot;} b={2} /&gt;

or spread

&lt;Component {...{a:&quot;a&quot;, b:2}} /&gt;

Note that the spread operator needs to be used, as {{a,b}} is not the same as {...{a,b}}

huangapple
  • 本文由 发表于 2023年5月28日 07:53:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定