Skip to content

Improve JSON types #373

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

Merged
merged 1 commit into from
Mar 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/BaseControllerV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ type primitive = null | boolean | number | string;

type DefinitelyNotJsonable = ((...args: any[]) => any) | undefined;

// Type copied from https://github.com/Microsoft/TypeScript/issues/1897#issuecomment-710744173
// Credit to https://github.com/grant-dennison for this type
// Source: https://github.com/Microsoft/TypeScript/issues/1897#issuecomment-710744173
export type IsJsonable<T> =
// Check if there are any non-jsonable types represented in the union
// Note: use of tuples in this first condition side-steps distributive conditional types
Expand All @@ -43,7 +44,11 @@ export type IsJsonable<T> =
? // It's an object
{
// Iterate over keys in object case
[P in keyof T]: IsJsonable<T[P]>; // Recursive call for children
[P in keyof T]: P extends string
? // Recursive call for children
IsJsonable<T[P]>
: // Exclude non-string keys
never;
}
: // Otherwise any other non-object no bueno
never
Expand Down Expand Up @@ -87,7 +92,7 @@ export interface StatePropertyMetadata<T> {
anonymous: boolean | StateDeriver<T>;
}

type Json = null | boolean | number | string | Json[] | { [prop: string]: Json } | Partial<Record<never, never>>;
type Json = null | boolean | number | string | Json[] | { [prop: string]: Json };
/**
* Controller class that provides state management, subscriptions, and state metadata
*/
Expand Down