1

我想知道是否可以输入自定义苗条商店的美元符号值?

这个例子

应用程序.svelte

<script>
    import { count } from './stores.js';
</script>

<h1>The count is {$count}</h1>

<button on:click={count.increment}>+</button>
<button on:click={count.decrement}>-</button>
<button on:click={count.reset}>reset</button>

商店.js

import { writable } from 'svelte/store';

function createCount() {
    const { subscribe, set, update } = writable(0);

    return {
        subscribe,
        increment: () => {},
        decrement: () => {},
        reset: () => {}
    };
}

export const count = createCount();

您如何{$count}使用自己的打字稿界面键入变量?

谢谢您的帮助

4

1 回答 1

5

您可以重命名stores.jsstores.ts并添加类型,例如:

import { Writable, writable } from "svelte/store"

import { writable } from "svelte/store"
type CountStore = {
  subscribe: Writable<number>["subscribe"]
    
  increment: () => void
  decrement: () => void
  reset: () => void
}

function createCount(): CountStore {
.
.
.

编辑:

您不需要定义类型,Typescript 做得很好,因此您只需将原始文件从 重命名jsts.

于 2021-01-13T14:40:00.293 回答