cosmology-tech / telescope

A TypeScript Transpiler for Cosmos Protobufs ⚛️
https://cosmology.zone/products/telescope
Apache License 2.0
146 stars 43 forks source link

fix(JsonSafe): handle nested structures and arrays #632

Closed 0xpatrickdev closed 2 months ago

0xpatrickdev commented 3 months ago
0xpatrickdev commented 3 months ago

I wasn't sure the best place for a test. Please see:

TS Playground

tsd test ```ts import { expectType } from 'tsd'; // Old version of JsonSafe type JsonSafeOld = { [Prop in keyof T]: T[Prop] extends Uint8Array | bigint | Date ? string : T[Prop]; }; // New version of JsonSafe export type JsonSafe = T extends Uint8Array | bigint | Date ? string : T extends Array ? Array> : T extends object ? { [K in keyof T]: JsonSafe } : T; const queryDelegatorUnbondingDelegationsResponse = { unbondingResponses: [ { entries: [ { creationHeight: BigInt(1), completionTime: { seconds: BigInt(123456789), nanos: 123456789, }, }, ], }, ], }; // Old JsonSafe doesn't handle nested bigint correctly type SafeUndelegationsQueryOld = JsonSafeOld; expectType<{ unbondingResponses: Array<{ entries: Array<{ creationHeight: bigint; // Still bigint, not converted to string completionTime: { seconds: bigint; // Still bigint, not converted to string }; }>; }>; }>(null as unknown as SafeUndelegationsQueryOld); // New JsonSafe handles nested bigint correctly type SafeUndelegationsQuery = JsonSafe; expectType<{ unbondingResponses: Array<{ entries: Array<{ creationHeight: string; // Correctly converted to string completionTime: { seconds: string; // Correctly converted to string }; }>; }>; }>(null as unknown as SafeUndelegationsQuery); const msgUndelegateResponse = { completionTime: { seconds: BigInt(123456789), nanos: 123456789, } }; // Old JsonSafe doesn't handle nested bigint correctly type SafeMsgUndelegateResponseOld = JsonSafeOld; expectType<{ completionTime: { seconds: bigint; // Still bigint, not converted to string }; }>(null as unknown as SafeMsgUndelegateResponseOld); // New JsonSafe handles nested bigint correctly type SafeMsgUndelegateResponse = JsonSafe; expectType<{ completionTime: { seconds: string; // Correctly converted to string }; }>(null as unknown as SafeMsgUndelegateResponse); ```