Skip to content

Commit 0c209d5

Browse files
committed
Fix
1 parent b3342d4 commit 0c209d5

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

pkgs/typed-api-spec/src/fetch/index.t-test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ type ValidateUrlTestCase = [
118118
(async () => {
119119
const f = fetch as FetchT<"", JsonSpec>;
120120
const f2 = fetch as FetchT<"", Spec>;
121-
const JSONT = JSON as JSONT;
122121
{
123122
// @ts-expect-error fetch requires input
124123
f();

pkgs/typed-api-spec/src/json/index.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,24 @@ type JsonPrimitive = string | number | boolean | null | Date;
2020
// eslint-disable-next-line @typescript-eslint/ban-types
2121
type InvalidJsonValue = undefined | Function | symbol | bigint;
2222

23-
type JsonifyArrayElement<T> = T extends InvalidJsonValue ? null : Jsonify<T>;
24-
2523
type JsonifyObject<T> = {
2624
[K in keyof T as K extends string
27-
? Jsonify<T[K]> extends infer ProcessedValue
28-
? ProcessedValue extends InvalidJsonValue
29-
? never
30-
: K
31-
: never
25+
? T[K] extends InvalidJsonValue
26+
? never
27+
: K
3228
: never]: Jsonify<T[K]>;
3329
};
3430

35-
// タプル型を保持するためのヘルパー型
36-
type JsonifyTuple<T extends readonly unknown[]> = {
37-
[K in keyof T]: T[K] extends InvalidJsonValue ? null : Jsonify<T[K]>;
38-
};
31+
// タプル型を保持するためのヘルパー型 - 再帰の深さを制限
32+
type JsonifyTuple<T extends readonly unknown[]> = T extends [
33+
infer First,
34+
...infer Rest,
35+
]
36+
? [
37+
First extends InvalidJsonValue ? null : Jsonify<First>,
38+
...JsonifyTuple<Rest>,
39+
]
40+
: [];
3941

4042
type Jsonify<T> = T extends { toJSON(): infer R }
4143
? Jsonify<R>
@@ -45,10 +47,10 @@ type Jsonify<T> = T extends { toJSON(): infer R }
4547
? T
4648
: T extends InvalidJsonValue
4749
? T
48-
: T extends readonly [unknown, ...unknown[]] // T may be tuple
50+
: T extends readonly [unknown, ...unknown[]] // タプル型の特別処理
4951
? JsonifyTuple<T>
50-
: T extends Array<infer E>
51-
? Array<JsonifyArrayElement<E>>
52+
: T extends (infer E)[]
53+
? (E extends InvalidJsonValue ? null : Jsonify<E>)[]
5254
: T extends object
5355
? JsonifyObject<T>
5456
: never;

0 commit comments

Comments
 (0)