Skip to content

Commit 7ce5281

Browse files
fix: recursive flattening for nested error unions (#347)
## Why Fixed a bug in the `flattenErrorType` utility where spreading a flattened union into another union was not working as expected. ## What changed - Fixed the `Flatten` type in `router/errors.ts` to properly handle nested unions (again) - Added a test case that demonstrates spreading a flattened union into another union - Bumped package version from 0.210.0 to 0.210.1 ## Versioning - [ ] Breaking protocol change - [ ] Breaking ts/js API change --------- Co-authored-by: Jacky Zhao <j.zhao2k19@gmail.com>
1 parent af3e09d commit 7ce5281

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

__tests__/typescript-stress.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,32 @@ describe('Procedure error schema', () => {
689689
),
690690
);
691691
});
692+
693+
test('spreading a flattened union into another union', () => {
694+
const BaseErrorSchema = flattenErrorType(
695+
Type.Union([
696+
Type.Object({
697+
code: Type.Literal('NOT_FOUND'),
698+
message: Type.String(),
699+
}),
700+
Type.Object({
701+
code: Type.Literal('PARSE_ERROR'),
702+
message: Type.String(),
703+
}),
704+
]),
705+
);
706+
707+
// Then we want to combine it with another error in a new union
708+
const ExtendedErrorSchema = Type.Union([
709+
Type.Object({
710+
code: Type.Literal('NEW_ERROR'),
711+
message: Type.String(),
712+
}),
713+
BaseErrorSchema,
714+
]);
715+
716+
acceptErrorSchema(ExtendedErrorSchema);
717+
});
692718
});
693719

694720
describe('fails', () => {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@replit/river",
33
"description": "It's like tRPC but... with JSON Schema Support, duplex streaming and support for service multiplexing. Transport agnostic!",
4-
"version": "0.210.0",
4+
"version": "0.210.1",
55
"type": "module",
66
"exports": {
77
".": {

router/errors.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,10 @@ function isUnion(schema: TSchema): schema is TUnion {
139139
return schema[Kind] === 'Union';
140140
}
141141

142-
type Flatten<T> = T extends BaseErrorSchemaType
142+
export type Flatten<T> = T extends BaseErrorSchemaType
143143
? T
144144
: T extends TUnion<Array<infer U extends TSchema>>
145-
? U extends BaseErrorSchemaType
146-
? TUnion<Array<U>>
147-
: Flatten<U>
145+
? Flatten<U>
148146
: unknown;
149147

150148
/**

0 commit comments

Comments
 (0)