Skip to content

Fixing more type hints

Compare
Choose a tag to compare
@razshare razshare released this 04 Oct 17:06
· 8 commits to main since this release

Changes

  • source and source::select no longer accept generics T, instead source::select::json and source::select::transform will each accept separate generics.
    Meaning you will now type your values like so with source::select::json
    <!-- +page.svelte -->
    <script lang="ts">
      import { source } from 'sveltekit-sse'
    
      type User = {
        username: string
      }
    
      const user = 
        source('/my-event')
          .select('user')
            .json<User>()
    </script>
    
    {#if $user}
      <h3>{$user.username}</h3>
    {/if}
    And with source::select::transform
    <!-- +page.svelte -->
    <script lang="ts">
      import { source } from 'sveltekit-sse'
    
      type User = {
        username: string
      }
    
      // For the sake of brevity
      // let's suppose all the event emits 
      // is the raw name of the user
      function transformer(raw:string):User{
        return {
          username: raw
        }
      }
    
      const user = 
        source('/my-event')
          .select('user')
            .transform<User>(transformer)
    </script>
    
    {#if $user}
      <h3>{$user.username}</h3>
    {/if}