Skip to content

Commit

Permalink
feat: Remove undefined from Jsonlike (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
Coobaha authored Nov 4, 2023
1 parent e512653 commit eb06e32
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 52 deletions.
8 changes: 5 additions & 3 deletions src/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type IsEqual<T, U> = (<G>() => G extends T ? 1 : 2) extends <G>() => G ex

type IsNotJsonableError<T> = Invalid<`${Extract<T, string>} is not Json-like`> & {};

type NotJsonable = ((...arguments_: any[]) => any) | undefined | symbol | RegExp | Function;
type NotJsonable = ((...arguments_: any[]) => any) | symbol | RegExp | Function;

type NeverToNull<T> = IsNever<T> extends true ? null : T;

Expand Down Expand Up @@ -66,8 +66,10 @@ export type Jsonlike<T, CastBehavior extends JsonCastBehavior> = T extends Posit
? JsonlikeList<WritableDeep<T>, CastBehavior>
: T extends object
? {
[K in keyof T]: [T[K]] extends [NotJsonable] | [never] ? IsNotJsonableError<K> : Jsonlike<T[K], CastBehavior>;
} // JsonifyObject recursive call for its children
[K in keyof T]: [T[K]] extends [NotJsonable] | [never] ? IsNotJsonableError<K> : Jsonlike<T[K], CastBehavior>; // JsonifyObject recursive call for its children
}
: T extends undefined
? T
: IsNotJsonableError<'Passed value'>;

export interface Invalid<msg = any> {
Expand Down
2 changes: 1 addition & 1 deletion test/test_schema.gen.json
Original file line number Diff line number Diff line change
Expand Up @@ -467,5 +467,5 @@
}
}
},
"$hash": "09d762cb9c53f4338c7374249078c983ab9ddf1a0814f98d7854a4e15f301e7b__v2.0.0"
"$hash": "09d762cb9c53f4338c7374249078c983ab9ddf1a0814f98d7854a4e15f301e7b__v2.0.0-rc.1"
}
99 changes: 51 additions & 48 deletions test/typed-fastify.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,52 +722,55 @@ expectType<Service<Params>>({
},
});

function verifyJsonlike<
Input,
Expected extends Jsonlike<Input, DoNotCastToPrimitive>,
DoNotCastToPrimitive extends 'cast' | 'combine',
>() {}

verifyJsonlike<
{
a: string;
b: {
toJSON(): number;
};
c: {
toJSON(): {
toJSON(): string;
function jsonLike<Input, DoNotCastToPrimitive extends 'cast' | 'combine'>() {
return {} as unknown as Jsonlike<Input, DoNotCastToPrimitive>;
}
expectType<{
a?: string;
aa?: string | null | undefined;
b: number;
c: string;
d: string;
A: Invalid<'A is not Json-like'>;
B: Invalid<'B is not Json-like'>;
C: Invalid<'C is not Json-like'>;
D?: undefined;
}>(
jsonLike<
{
a: string | undefined;
aa?: string | null | undefined;
b: {
toJSON(): number;
};
};
d: Date;
A: RegExp;
B: Function;
C: () => 1;
D: undefined;
},
{
a: string;
b: number;
c: string;
d: string;
A: Invalid<'A is not Json-like'>;
B: Invalid<'B is not Json-like'>;
C: Invalid<'C is not Json-like'>;
D: Invalid<'D is not Json-like'>;
},
'cast'
>();

verifyJsonlike<
{
a: Date;
b: Number;
A: RegExp;
},
{
a: Date;
b: number;
A: Invalid<'A is not Json-like'>;
},
'combine'
>();
c: {
toJSON(): {
toJSON(): string;
};
};
d: Date;
A: RegExp;
B: Function;
C: () => 1;
D: undefined;
},
'cast'
>(),
);

expectType<{
a: Date | string;
aa?: Date | string;
b: number;
A: Invalid<'A is not Json-like'>;
}>(
jsonLike<
{
a: Date;
aa: Date | undefined;
b: Number;
A: RegExp;
},
'combine'
>(),
);

0 comments on commit eb06e32

Please sign in to comment.