sveltejs / svelte

Cybernetically enhanced web apps
https://svelte.dev
MIT License
76.8k stars 3.98k forks source link

Svelte5: Improve type definitions for runes #11437

Closed ottomated closed 2 weeks ago

ottomated commented 2 weeks ago

Describe the problem

Typescript's intellisense lists every single function prototype property on runes:

image

This makes it difficult to see the actual useful properties (frozen, snapshot) and allows you to write things like

$state.bind(this);

without Typescript complaining.

Describe the proposed solution

The type definitions can be updated like so:


declare const $state: {
    /** @deprecated */
    apply: never;
    /** @deprecated */
    arguments: never;
    /** @deprecated */
    bind: never;
    /** @deprecated */
    call: never;
    /** @deprecated */
    caller: never;
    /** @deprecated */
    length: never;
    /** @deprecated */
    name: never;
    /** @deprecated */
    prototype: never;
    /** @deprecated */
    toString: never;

    <T>(initial: T): T;
    <T>(): T | undefined;

    frozen<T>(initial: T): Readonly<T>;
    frozen<T>(): Readonly<T> | undefined;

    snapshot<T>(state: T): T;
};

which makes intellisense look like this:

image

with this tradeoff:

image

(note that I explored a few ways of achieving this and it seems fairly delicate)

Importance

would make my life easier

AdrianGonz97 commented 2 weeks ago

Pretty clever! Riffing off of this a bit, perhaps this could work too? REPL

Applying it to the namespace instead should curb the tradeoff issue altogether: img