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

feat(testlab): improve typings for toJSON() helper #3283

Merged
merged 1 commit into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
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
46 changes: 46 additions & 0 deletions packages/testlab/src/__tests__/unit/to-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,42 @@ describe('toJSON', () => {
expectUndefined(value);
});

it('handles `object | null`', () => {
const input = createValueOfUnionType<object | null>({});
const output = toJSON(input);
expectComplexType<object | null>(output, input);
});

it('handles `object | undefined`', () => {
const input = createValueOfUnionType<object | undefined>({});
const output = toJSON(input);
expectComplexType<object | undefined>(output, input);
});

it('handles `object | null | undefined`', () => {
const input = createValueOfUnionType<object | null | undefined>({});
const output = toJSON(input);
expectComplexType<object | null | undefined>(output, input);
});

it('handles `unknown[] | null`', () => {
const input = createValueOfUnionType<unknown[] | null>([]);
const output = toJSON(input);
expectComplexType<unknown[] | null>(output, input);
});

it('handles `unknown[] | undefined`', () => {
const input = createValueOfUnionType<unknown[] | undefined>([]);
const output = toJSON(input);
expectComplexType<unknown[] | undefined>(output, input);
});

it('handles `unknown[] | null | undefined`', () => {
const input = createValueOfUnionType<unknown[] | null | undefined>([]);
const output = toJSON(input);
expectComplexType<unknown[] | null | undefined>(output, input);
});

it('handles classes with custom toJSON', () => {
class Customer {
private __data: object;
Expand Down Expand Up @@ -156,3 +192,13 @@ function expectNull(actual: null) {
function expectUndefined(actual: undefined) {
expect(actual).to.be.undefined();
}

function expectComplexType<T>(actual: T, expected: T) {
expect(actual).to.deepEqual(expected);
}

// A helper to force TypeScript to treat the given value as of a union type,
// e.g. treat `{}` as `object | undefined | null`.
function createValueOfUnionType<T>(value: T): T {
return value;
}
15 changes: 15 additions & 0 deletions packages/testlab/src/to-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ export function toJSON(value: number): number;
export function toJSON(value: boolean): boolean;
export function toJSON(value: string): string;

// The following overloads are required to allow TypesScript handle
// commonly used union types.

export function toJSON(value: unknown[] | null): unknown[] | null;
export function toJSON(value: unknown[] | undefined): unknown[] | undefined;
export function toJSON(
value: unknown[] | null | undefined,
): unknown[] | null | undefined;

export function toJSON(value: object | null): object | null;
export function toJSON(value: object | undefined): object | undefined;
export function toJSON(
value: object | null | undefined,
): object | null | undefined;

export function toJSON<T>(value: T) {
return JSON.parse(JSON.stringify({value})).value;
}