Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-things-wear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: allow optional types for remote query/command/prerender functions
10 changes: 7 additions & 3 deletions packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2110,7 +2110,7 @@ export type RemoteForm<Input extends RemoteFormInput | void, Output> = {
* The return value of a remote `command` function. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#command) for full documentation.
*/
export type RemoteCommand<Input, Output> = {
(arg: Input): Promise<Awaited<Output>> & {
(arg: undefined extends Input ? Input | void : Input): Promise<Awaited<Output>> & {
updates(...queries: Array<RemoteQuery<any> | RemoteQueryOverride>): Promise<Awaited<Output>>;
};
/** The number of pending command executions */
Expand Down Expand Up @@ -2180,11 +2180,15 @@ export interface RemoteQueryOverride {
/**
* The return value of a remote `prerender` function. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#prerender) for full documentation.
*/
export type RemotePrerenderFunction<Input, Output> = (arg: Input) => RemoteResource<Output>;
export type RemotePrerenderFunction<Input, Output> = (
arg: undefined extends Input ? Input | void : Input
) => RemoteResource<Output>;

/**
* The return value of a remote `query` function. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#query) for full documentation.
*/
export type RemoteQueryFunction<Input, Output> = (arg: Input) => RemoteQuery<Output>;
export type RemoteQueryFunction<Input, Output> = (
arg: undefined extends Input ? Input | void : Input
) => RemoteQuery<Output>;

export * from './index.js';
46 changes: 46 additions & 0 deletions packages/kit/test/types/remote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {

const schema: StandardSchemaV1<string> = null as any;
const schema2: StandardSchemaV1<string, number> = null as any;
const schema3: StandardSchemaV1<string | undefined, number> = null as any;

function query_tests() {
const no_args: RemoteQueryFunction<void, string> = query(() => 'Hello world');
Expand Down Expand Up @@ -40,6 +41,21 @@ function query_tests() {
}
void query_without_args();

async function query_with_optional_arg() {
const q = query(schema3, () => 'Hello world');
void q();
void q('hi');
// @ts-expect-error
void q(1);

const q2 = query('unchecked', (a?: string) => a);
void q2();
void q2('hi');
// @ts-expect-error
void q2(1);
}
void query_with_optional_arg();

async function query_unsafe() {
const q = query('unchecked', (a: number) => a);
const result: number = await q(1);
Expand Down Expand Up @@ -106,6 +122,21 @@ function prerender_tests() {
}
void prerender_unsafe();

async function prerender_with_optional_arg() {
const q = prerender(schema3, () => 'Hello world');
void q();
void q('hi');
// @ts-expect-error
void q(1);

const q2 = prerender('unchecked', (a?: string) => a);
void q2();
void q2('hi');
// @ts-expect-error
void q2(1);
}
void prerender_with_optional_arg();

async function prerender_schema() {
const q = prerender(schema, (a) => a);
const result: string = await q('1');
Expand Down Expand Up @@ -141,6 +172,21 @@ function command_tests() {
}
void command_without_args();

async function command_with_optional_arg() {
const q = command(schema3, () => 'Hello world');
void q();
void q('hi');
// @ts-expect-error
void q(1);

const q2 = command('unchecked', (a?: string) => a);
void q2();
void q2('hi');
// @ts-expect-error
void q2(1);
}
void command_with_optional_arg();

async function command_unsafe() {
const cmd = command('unchecked', (a: string) => a);
const result: string = await cmd('test');
Expand Down
10 changes: 7 additions & 3 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2085,7 +2085,7 @@ declare module '@sveltejs/kit' {
* The return value of a remote `command` function. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#command) for full documentation.
*/
export type RemoteCommand<Input, Output> = {
(arg: Input): Promise<Awaited<Output>> & {
(arg: undefined extends Input ? Input | void : Input): Promise<Awaited<Output>> & {
updates(...queries: Array<RemoteQuery<any> | RemoteQueryOverride>): Promise<Awaited<Output>>;
};
/** The number of pending command executions */
Expand Down Expand Up @@ -2155,12 +2155,16 @@ declare module '@sveltejs/kit' {
/**
* The return value of a remote `prerender` function. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#prerender) for full documentation.
*/
export type RemotePrerenderFunction<Input, Output> = (arg: Input) => RemoteResource<Output>;
export type RemotePrerenderFunction<Input, Output> = (
arg: undefined extends Input ? Input | void : Input
) => RemoteResource<Output>;

/**
* The return value of a remote `query` function. See [Remote functions](https://svelte.dev/docs/kit/remote-functions#query) for full documentation.
*/
export type RemoteQueryFunction<Input, Output> = (arg: Input) => RemoteQuery<Output>;
export type RemoteQueryFunction<Input, Output> = (
arg: undefined extends Input ? Input | void : Input
) => RemoteQuery<Output>;
interface AdapterEntry {
/**
* A string that uniquely identifies an HTTP service (e.g. serverless function) and is used for deduplication.
Expand Down