So new to svelte but it is so small it is perfect for a job i am working on.
Went for the typescript option: https://svelte.dev/blog/svelte-and-typescript
How or where can i find the types for custom component events:
A simple login component form:
<script lang="ts">
import { createEventDispatcher } from 'svelte'
const dispatch = createEventDispatcher()
let isSubmitting = false
const handleSubmit = (e: HTMLFormElement) => {
e.preventDefault()
isSubmitting = true
const payload = {
username: e.target.username.value,
password: e.target.password.value,
}
dispatch('submit', payload)
}
</script>
<form on:submit={handleSubmit}>
<label for="username"><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="username" required id="username">
<label for="password"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="password" required id="password">
<button type="submit" disabled={isSubmitting}>Login</button>
</form>
Included in another component to handle the submit:
<script lang="ts">
import Login from './molecules/Login.svelte'
const loginHandle = function (a: any) {
console.log(a)
}
</script>
<main class="{open}">
{#if !authenticated}
<Login on:submit={loginHandle}/>
{/if}
</main>
Right now there is an ugly any
added to the loginHandle
but when dumping the event to console it looks to be very svelte specific.. where can i find the type?
You can pass an object to the generic of the
createEventDispatcher
function. Where key is a custom event name and value is a detail object.Child component (
<Calendar />
)Parent component
Usage example