razshare / sveltekit-sse

Server Sent Events with SvelteKit
https://www.npmjs.com/package/sveltekit-sse
MIT License
306 stars 9 forks source link

TypeScript type support for .json method #31

Closed unknownsoldier08 closed 8 months ago

unknownsoldier08 commented 8 months ago

Is there any example on how to correctly type the .json() method using TypeScript?

razshare commented 8 months ago

Hi @unknownsoldier08 ,

I just released version 0.8.9 with better type hinting support for .json(), please update to the latest version.

You should now get proper documentation in your editor. image

A full example using typescript would look like this

<script lang="ts">
  import { source } from 'sveltekit-sse';
  const channel = source('/events').select('notification');
  const notification = channel.json<{ content: string }>();
</script>

{$notification?.content}

And with error management

<script lang="ts">
  import { source } from 'sveltekit-sse';
  const channel = source('/events').select('notification');
  const notification = channel.json<{ content: string }>(function onError({ previous, error }) {
    console.error(error);
    return previous;
  });
</script>

{$notification?.content}

Let me know if this is what you're looking for.

unknownsoldier08 commented 8 months ago

That works, thank you!