Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 11, 2024

Conversation

0xpatrickdev
Copy link
Contributor

  • Update JsonSafe<T> type to recursively handle nested objects and arrays

- Update JsonSafe<T> type to recursively handle nested objects and arrays
@0xpatrickdev
Copy link
Contributor Author

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

TS Playground

tsd test
import { expectType } from 'tsd';

// Old version of JsonSafe
type JsonSafeOld<T> = {
  [Prop in keyof T]: T[Prop] extends Uint8Array | bigint | Date ? string : T[Prop];
};

// New version of JsonSafe
export type JsonSafe<T> = T extends Uint8Array | bigint | Date
  ? string
  : T extends Array<infer U>
  ? Array<JsonSafe<U>>
  : T extends object
  ? { [K in keyof T]: JsonSafe<T[K]> }
  : 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<typeof queryDelegatorUnbondingDelegationsResponse>;
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<typeof queryDelegatorUnbondingDelegationsResponse>;
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<typeof msgUndelegateResponse>;
expectType<{
  completionTime: {
    seconds: bigint; // Still bigint, not converted to string
  };
}>(null as unknown as SafeMsgUndelegateResponseOld);

// New JsonSafe handles nested bigint correctly
type SafeMsgUndelegateResponse = JsonSafe<typeof msgUndelegateResponse>;
expectType<{
  completionTime: {
    seconds: string; // Correctly converted to string
  };
}>(null as unknown as SafeMsgUndelegateResponse);

@Zetazzz Zetazzz merged commit 326289e into cosmology-tech:main Jul 11, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants