Skip to content

Commit e1b326b

Browse files
GauBeneltigerchino
andauthored
fix: correctly type remote functions (#14098)
* fix(kit): correctly type remote functions * add test * Create brown-geckos-end.md * Update .changeset/brown-geckos-end.md * get rid of errors --------- Co-authored-by: Chew Tee Ming <[email protected]>
1 parent 9e132c7 commit e1b326b

File tree

6 files changed

+54
-41
lines changed

6 files changed

+54
-41
lines changed

.changeset/brown-geckos-end.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/kit": patch
3+
---
4+
5+
fix: correctly type remote function input parameters from a schema

packages/kit/src/runtime/app/server/remote/command.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import { get_event_state } from '../../../server/event-state.js';
3939
* @overload
4040
* @param {Schema} validate
4141
* @param {(arg: StandardSchemaV1.InferOutput<Schema>) => Output} fn
42-
* @returns {RemoteCommand<StandardSchemaV1.InferOutput<Schema>, Output>}
42+
* @returns {RemoteCommand<StandardSchemaV1.InferInput<Schema>, Output>}
4343
* @since 2.27
4444
*/
4545
/**

packages/kit/src/runtime/app/server/remote/prerender.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ import { get_event_state } from '../../../server/event-state.js';
5151
* @overload
5252
* @param {Schema} schema
5353
* @param {(arg: StandardSchemaV1.InferOutput<Schema>) => MaybePromise<Output>} fn
54-
* @param {{ inputs?: RemotePrerenderInputsGenerator<StandardSchemaV1.InferOutput<Schema>>, dynamic?: boolean }} [options]
55-
* @returns {RemotePrerenderFunction<StandardSchemaV1.InferOutput<Schema>, Output>}
54+
* @param {{ inputs?: RemotePrerenderInputsGenerator<StandardSchemaV1.InferInput<Schema>>, dynamic?: boolean }} [options]
55+
* @returns {RemotePrerenderFunction<StandardSchemaV1.InferInput<Schema>, Output>}
5656
* @since 2.27
5757
*/
5858
/**

packages/kit/src/runtime/app/server/remote/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import { get_event_state } from '../../../server/event-state.js';
4646
* @overload
4747
* @param {Schema} schema
4848
* @param {(arg: StandardSchemaV1.InferOutput<Schema>) => MaybePromise<Output>} fn
49-
* @returns {RemoteQueryFunction<StandardSchemaV1.InferOutput<Schema>, Output>}
49+
* @returns {RemoteQueryFunction<StandardSchemaV1.InferInput<Schema>, Output>}
5050
* @since 2.27
5151
*/
5252
/**

packages/kit/test/types/remote.test.ts

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ import { StandardSchemaV1 } from '@standard-schema/spec';
33
import { RemotePrerenderFunction, RemoteQueryFunction } from '@sveltejs/kit';
44

55
const schema: StandardSchemaV1<string> = null as any;
6+
const schema2: StandardSchemaV1<string, number> = null as any;
67

78
function query_tests() {
89
const no_args: RemoteQueryFunction<void, string> = query(() => 'Hello world');
9-
no_args();
10+
void no_args();
1011
// @ts-expect-error
11-
no_args('');
12+
void no_args('');
1213

1314
const one_arg: RemoteQueryFunction<number, string> = query('unchecked', (a: number) =>
1415
a.toString()
1516
);
16-
one_arg(1);
17+
void one_arg(1);
1718
// @ts-expect-error
18-
one_arg('1');
19+
void one_arg('1');
1920
// @ts-expect-error
20-
one_arg();
21+
void one_arg();
2122

2223
async function query_without_args() {
2324
const q = query(() => 'Hello world');
@@ -27,45 +28,52 @@ function query_tests() {
2728
const wrong: number = await q();
2829
wrong;
2930
// @ts-expect-error
30-
q(1);
31+
void q(1);
3132
// @ts-expect-error
32-
query((a: string) => 'hi');
33+
query((_: string) => 'hi');
3334
}
34-
query_without_args();
35+
void query_without_args();
3536

3637
async function query_unsafe() {
3738
const q = query('unchecked', (a: number) => a);
3839
const result: number = await q(1);
3940
result;
4041
// @ts-expect-error
41-
q(1, 2, 3);
42+
void q(1, 2, 3);
4243
// @ts-expect-error
43-
q('1', '2');
44+
void q('1', '2');
4445
}
45-
query_unsafe();
46+
void query_unsafe();
4647

47-
async function query_schema() {
48+
async function query_schema_input_only() {
4849
const q = query(schema, (a) => a);
4950
const result: string = await q('1');
5051
result;
5152
}
52-
query_schema();
53+
void query_schema_input_only();
54+
55+
async function query_schema_input_and_output() {
56+
const q = query(schema2, (a) => a);
57+
const result: number = await q('1');
58+
result;
59+
}
60+
void query_schema_input_and_output();
5361
}
5462
query_tests();
5563

5664
function prerender_tests() {
5765
const no_args: RemotePrerenderFunction<void, string> = prerender(() => 'Hello world');
58-
no_args();
66+
void no_args();
5967
// @ts-expect-error
60-
no_args('');
68+
void no_args('');
6169
const one_arg: RemotePrerenderFunction<number, string> = prerender('unchecked', (a: number) =>
6270
a.toString()
6371
);
64-
one_arg(1);
72+
void one_arg(1);
6573
// @ts-expect-error
66-
one_arg('1');
74+
void one_arg('1');
6775
// @ts-expect-error
68-
one_arg();
76+
void one_arg();
6977

7078
async function prerender_without_args() {
7179
const q = prerender(() => 'Hello world');
@@ -75,31 +83,31 @@ function prerender_tests() {
7583
const wrong: number = await q();
7684
wrong;
7785
// @ts-expect-error
78-
q(1);
86+
void q(1);
7987
// @ts-expect-error
80-
query((a: string) => 'hi');
88+
query((_: string) => 'hi');
8189
}
82-
prerender_without_args();
90+
void prerender_without_args();
8391

8492
async function prerender_unsafe() {
8593
const q = prerender('unchecked', (a: number) => a);
8694
const result: number = await q(1);
8795
result;
8896
// @ts-expect-error
89-
q(1, 2, 3);
97+
void q(1, 2, 3);
9098
// @ts-expect-error
91-
q('1', '2');
99+
void q('1', '2');
92100
}
93-
prerender_unsafe();
101+
void prerender_unsafe();
94102

95103
async function prerender_schema() {
96104
const q = prerender(schema, (a) => a);
97105
const result: string = await q('1');
98106
result;
99107
}
100-
prerender_schema();
108+
void prerender_schema();
101109

102-
async function prerender_schema_entries() {
110+
function prerender_schema_entries() {
103111
const q = prerender(schema, (a) => a, { inputs: () => ['1'] });
104112
q;
105113
// @ts-expect-error
@@ -125,31 +133,31 @@ function command_tests() {
125133
const wrong: number = await cmd();
126134
wrong;
127135
}
128-
command_without_args();
136+
void command_without_args();
129137

130138
async function command_unsafe() {
131139
const cmd = command('unchecked', (a: string) => a);
132140
const result: string = await cmd('test');
133141
result;
134142
// @ts-expect-error
135-
cmd(1);
143+
void cmd(1);
136144
// @ts-expect-error
137-
cmd('1', 2);
145+
void cmd('1', 2);
138146
}
139-
command_unsafe();
147+
void command_unsafe();
140148

141149
async function command_schema() {
142150
const cmd = command(schema, (a) => a);
143151
const result: string = await cmd('foo');
144152
result;
145153
// @ts-expect-error
146-
cmd(123);
154+
void cmd(123);
147155
}
148-
command_schema();
156+
void command_schema();
149157
}
150158
command_tests();
151159

152-
async function form_tests() {
160+
function form_tests() {
153161
const q = query(() => '');
154162
const f = form((f) => {
155163
f.get('');

packages/kit/types/index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,7 +2652,7 @@ declare module '$app/server' {
26522652
*
26532653
* @since 2.27
26542654
*/
2655-
export function command<Schema extends StandardSchemaV1, Output>(validate: Schema, fn: (arg: StandardSchemaV1.InferOutput<Schema>) => Output): RemoteCommand<StandardSchemaV1.InferOutput<Schema>, Output>;
2655+
export function command<Schema extends StandardSchemaV1, Output>(validate: Schema, fn: (arg: StandardSchemaV1.InferOutput<Schema>) => Output): RemoteCommand<StandardSchemaV1.InferInput<Schema>, Output>;
26562656
/**
26572657
* Creates a form object that can be spread onto a `<form>` element.
26582658
*
@@ -2691,9 +2691,9 @@ declare module '$app/server' {
26912691
* @since 2.27
26922692
*/
26932693
export function prerender<Schema extends StandardSchemaV1, Output>(schema: Schema, fn: (arg: StandardSchemaV1.InferOutput<Schema>) => MaybePromise<Output>, options?: {
2694-
inputs?: RemotePrerenderInputsGenerator<StandardSchemaV1.InferOutput<Schema>>;
2694+
inputs?: RemotePrerenderInputsGenerator<StandardSchemaV1.InferInput<Schema>>;
26952695
dynamic?: boolean;
2696-
} | undefined): RemotePrerenderFunction<StandardSchemaV1.InferOutput<Schema>, Output>;
2696+
} | undefined): RemotePrerenderFunction<StandardSchemaV1.InferInput<Schema>, Output>;
26972697
/**
26982698
* Creates a remote query. When called from the browser, the function will be invoked on the server via a `fetch` call.
26992699
*
@@ -2717,7 +2717,7 @@ declare module '$app/server' {
27172717
*
27182718
* @since 2.27
27192719
*/
2720-
export function query<Schema extends StandardSchemaV1, Output>(schema: Schema, fn: (arg: StandardSchemaV1.InferOutput<Schema>) => MaybePromise<Output>): RemoteQueryFunction<StandardSchemaV1.InferOutput<Schema>, Output>;
2720+
export function query<Schema extends StandardSchemaV1, Output>(schema: Schema, fn: (arg: StandardSchemaV1.InferOutput<Schema>) => MaybePromise<Output>): RemoteQueryFunction<StandardSchemaV1.InferInput<Schema>, Output>;
27212721
type RemotePrerenderInputsGenerator<Input = any> = () => MaybePromise<Input[]>;
27222722
type MaybePromise<T> = T | Promise<T>;
27232723

0 commit comments

Comments
 (0)