Skip to content

Commit 6caa020

Browse files
authored
feat: improve type coverage for InferParam (#43)
1 parent 2c6580e commit 6caa020

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

src/context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
Prettify,
88
UnionToIntersection,
99
} from "./helper";
10-
import type { Middleware, MiddlewareOptions } from "./middleware";
10+
import type { Middleware, MiddlewareContext, MiddlewareOptions } from "./middleware";
1111
import { runValidation } from "./validator";
1212
import {
1313
getCookieKey,
@@ -107,7 +107,7 @@ export type InferInputMethod<
107107
export type InferParam<Path extends string> = IsEmptyObject<
108108
InferParamPath<Path> & InferParamWildCard<Path>
109109
> extends true
110-
? Record<string, any> | undefined
110+
? {}
111111
: Prettify<InferParamPath<Path> & InferParamWildCard<Path>>;
112112

113113
export type InferParamInput<Path extends string> = IsEmptyObject<

src/helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ export type InferParamWildCard<Path> = Path extends
4343
: Path extends `${infer _Start}/*`
4444
? { [K in "_"]: string }
4545
: Path extends `${infer _Start}/${infer Rest}`
46-
? InferParamPath<Rest>
46+
? InferParamWildCard<Rest>
4747
: {};

src/type.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { describe, expectTypeOf, it } from "vitest";
2+
import type { InferParam } from "./context";
3+
import type { InferParamPath, InferParamWildCard } from "./helper";
4+
5+
describe("infer param", () => {
6+
it("empty path", () => {
7+
expectTypeOf<InferParamPath<"/">>().toEqualTypeOf<{}>();
8+
expectTypeOf<InferParamWildCard<"/">>().toEqualTypeOf<{}>();
9+
expectTypeOf<InferParam<"/">>().toEqualTypeOf<{}>();
10+
});
11+
it("static path", () => {
12+
expectTypeOf<InferParamPath<"/static/path">>().toEqualTypeOf<{}>();
13+
expectTypeOf<InferParamWildCard<"/static/path">>().toEqualTypeOf<{}>();
14+
expectTypeOf<InferParam<"/static/path">>().toEqualTypeOf<{}>();
15+
});
16+
it("single param", () => {
17+
expectTypeOf<InferParamPath<"/user/:id">>().toEqualTypeOf<{ id: string }>();
18+
expectTypeOf<InferParamWildCard<"/user/:id">>().toEqualTypeOf<{}>();
19+
expectTypeOf<InferParam<"/user/:id">>().toEqualTypeOf<{ id: string }>();
20+
});
21+
it("multiple params", () => {
22+
expectTypeOf<InferParamPath<"/user/:userId/post/:postId">>().toEqualTypeOf<{
23+
userId: string;
24+
postId: string;
25+
}>();
26+
expectTypeOf<InferParamWildCard<"/user/:userId/post/:postId">>().toEqualTypeOf<{}>();
27+
expectTypeOf<InferParam<"/user/:userId/post/:postId">>().toEqualTypeOf<{
28+
userId: string;
29+
postId: string;
30+
}>();
31+
});
32+
it("wildcard param", () => {
33+
expectTypeOf<InferParamPath<"/files/*">>().toEqualTypeOf<{}>();
34+
expectTypeOf<InferParamWildCard<"/files/*">>().toEqualTypeOf<{ _: string }>();
35+
expectTypeOf<InferParam<"/files/*">>().toEqualTypeOf<{ _: string }>();
36+
});
37+
it("mixed params", () => {
38+
expectTypeOf<InferParamPath<"/user/:userId/files/*">>().toEqualTypeOf<{ userId: string }>();
39+
expectTypeOf<InferParamWildCard<"/user/:userId/files/*">>().toEqualTypeOf<{ _: string }>();
40+
expectTypeOf<InferParam<"/user/:userId/files/*">>().toEqualTypeOf<{
41+
userId: string;
42+
_: string;
43+
}>();
44+
});
45+
});

0 commit comments

Comments
 (0)