-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Svelte 5: Variadic snippets #9672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You can remove the |
There's one caveat with ambiguity: |
Maybe it should just be simplified to always require an array generic? type Snippet<Args extends unknown[] = []> = (this: void, ...args: Args) => unknown;
type ObjectSnippet = Snippet<[{ name: string, age: number }]>;
type TupleSnippet = Snippet<[string, number, boolean, ...boolean[]]>;
type BooleanRestSnippet = Snippet<[...boolean[]]>;
type BooleanArraySnippet = Snippet<[boolean[]]>;
type OptionalArgSnippet = Snippet<
[string, number, boolean?]
>; Reduces ambiguity as you'll always need to type the function arguments based on their position in the arguments list. Just has the tradeoff of being a bit less pretty for the single argument cases. EDIT: type BooleanRestSnippet = Snippet<boolean[]>; but it's still less ambiguous once you realise it's always an array of function args. |
I raise you all the version that works with tuples, single-argument-typed-as-an-array, and objects + primitives as a single argument: type Snippet<Args = any> =
Args extends any[]
// it's an array
? number extends Args['length']
// it's an array, not a tuple
? (this: void, arg: Args) => unknown
// it's a tuple (because the type of Args['length'] is narrower than `number`)
: (this: void, ...args: Args) => unknown
// it's anything else
: (this: void, arg: Args) => unknown;
type MyCustomSnippet = Snippet<[firstName: string, lastName: string, dob: Date]>;
type MyCustomSnippetWithOneArg = Snippet<{ firstName: string, lastName: string, dob: Date }>;
type MyPreviouslyBrokenSnippet = Snippet<boolean[]> |
@tcc-sejohnson this prevents being able to define snippets with single rest arguments AFAICT though. e.g. type MyBrokenSnippet = Snippet<[...boolean[]]>
// ^? (this: void, arg: boolean[]) => unknown I'd probably go with your original type over this one for that reason, though I'm still leaning towards the generic always being an array that maps to the function args as the most practical/unambiguous solution. |
Rats 😆 yeah, guess I don't have a solution for that one. I kinda agree with you, there's something nice about just: type Snippet<Args = any> = (this: void, ...args: Args) => unknown;
type MyCustomSnippet = Snippet<[todos: Todo[]]>; It's an extra pair of brackets in the absolute simplest case, but it gives you just one way to do things... |
Good to see this is completed now! Edit: I just saw the updated documentation, |
|
Describe the problem
Snippets take a single argument. This is for two reasons:
Describe the proposed solution
Hat-tip to @tcc-sejohnson for solving the typing issue:
I haven't thought deeply about the optimisation question. It may be a non-issue.
Alternatives considered
not doing this
Importance
nice to have
The text was updated successfully, but these errors were encountered: