GithubHelp home page GithubHelp logo

Comments (4)

ciscoheat avatar ciscoheat commented on May 29, 2024

Hey, glad you like it! :) I'd handle that with a reactive statement just below the flash initialization, like this:

import { initFlash } from 'sveltekit-flash-message/client';
import { page } from '$app/stores';

const flash = initFlash(page);

const timeoutMs = 5000;
let flashTimeout: ReturnType<typeof setTimeout>;

$: if ($flash) {
  clearTimeout(flashTimeout);
  flashTimeout = setTimeout(() => ($flash = undefined), timeoutMs);
}

I will add that to the readme!

from sveltekit-flash-message.

mike-lloyd03 avatar mike-lloyd03 commented on May 29, 2024

Works perfectly! Thank you!

from sveltekit-flash-message.

mike-lloyd03 avatar mike-lloyd03 commented on May 29, 2024

One other question. When sending the flash message, I'm following the Readme example:

    const message = { type: 'success', message: "Logged in" };
    throw redirect(message, event);

But doing this raises the following warning:

Argument of type '{ type: string; message: string; }' is not assignable to parameter of type '{ type: "success" | "error"; message: string; }'.
Types of property 'type' are incompatible.
Type 'string' is not assignable to type '"success" | "error"'.

My solution is this:

    const type: "success" = "success";
    const message = { type, message: "Logged in" };
    throw redirect(message, event);

I'm new to Typescript but is this the idiomatic way to do this?

from sveltekit-flash-message.

ciscoheat avatar ciscoheat commented on May 29, 2024

Yes, the problem is that when you create message as a separate variable, typescript widens the type of string literals to string, so it doesn't match the literal type of redirect.message.

Two solutions:

// Add 'as const' to make the type literal
const message = { type: 'success', message: "Logged in" } as const;
throw redirect(message, event);

// Or pass it directly as a parameter, so it won't be widened by the compiler
throw redirect({ type: 'success', message: "Logged in" }, event);

Read more about type widening/narrowing here: https://dev.to/toluagboola/type-widening-and-narrowing-in-typescript-5ejo

Readme updated with this, thanks for the notice!

from sveltekit-flash-message.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.