Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/stolinski/svelte-typescript-cheetsheet


https://github.com/stolinski/svelte-typescript-cheetsheet

Last synced: 12 days ago
JSON representation

Awesome Lists containing this project

README

        


Svelte+TypeScript Cheatsheets




### Typing Props

* required props are just defined like normal with a : to inidicate the type.
* optional props are the same, however you need to set a default value.

Basically, if need a prop to be optional, you need to assign it a value when defined instead of using `let value?: string`

```html

export let someProp: string // Required prop
export let someOtherProp: string = "hello" // Optional prop

```

### Stores

```javascript

interface BlogPost {
status: "ACTIVE" | "HIDDEN"
}

const post = writable({
status: 'ACTIVE',
})
```

To type an expected store, like passing a store as a prop, you would use the Writable generic type.

```javascript
import type { Writable } from 'svelte/store';

export let data: Writable

```

### Events

```javascript
const dispatch = createEventDispatcher<{ nameOfEvent: { passedProperties: any } }>()
```

here is the same thing with real code.

```javascript
const dispatch = createEventDispatcher<{ toggle: { isToggled: boolean } }>()

// Now I'm typed!
dispatch('toggle', {
isToggled,
})

```

### Types in Svelte Template Code

This is not currently supported.

### JSDoc Type Checking

For now, see: https://www.swyx.io/jsdoc-swyxkit/