Skip to content

Commit 4f5a305

Browse files
authored
fix(types): fix 4.9.8 regression (#4448)
* move error check * add test * format
1 parent cc8868d commit 4f5a305

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/utils/types.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ describe('JSONParsed', () => {
6060
type Expected = { a: number }
6161
expectTypeOf<Actual>().toEqualTypeOf<Expected>()
6262
})
63+
it('should convert invalid type with { toJSON() => T to T', () => {
64+
type Actual = JSONParsed<bigint & { toJSON(): string }>
65+
type Expected = string
66+
expectTypeOf<Actual>().toEqualTypeOf<Expected>()
67+
})
6368
})
6469

6570
describe('invalid types', () => {

src/utils/types.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,9 @@ export type JSONValue = JSONObject | JSONArray | JSONPrimitive
5454
* which defaults to `bigint | ReadonlyArray<bigint>`.
5555
* You can set it to `never` to disable this check.
5656
*/
57-
export type JSONParsed<T, TError = bigint | ReadonlyArray<bigint>> = T extends TError
58-
? never
59-
: T extends {
60-
toJSON(): infer J
61-
}
57+
export type JSONParsed<T, TError = bigint | ReadonlyArray<bigint>> = T extends {
58+
toJSON(): infer J
59+
}
6260
? (() => J) extends () => JSONPrimitive
6361
? J
6462
: (() => J) extends () => { toJSON(): unknown }
@@ -83,7 +81,9 @@ export type JSONParsed<T, TError = bigint | ReadonlyArray<bigint>> = T extends T
8381
: JSONParsed<T[K], TError>
8482
}
8583
: T extends unknown
86-
? JSONValue
84+
? T extends TError
85+
? never
86+
: JSONValue
8787
: never
8888

8989
/**

0 commit comments

Comments
 (0)