Skip to content

Commit

Permalink
test: add test unit
Browse files Browse the repository at this point in the history
  • Loading branch information
ShanaMaid committed May 7, 2019
1 parent 42f36a0 commit f36066b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/__snapshots__/mapped-types.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ exports[`OmitByValueExact testType<OmitByValueExact<RequiredOptionalProps, undef
exports[`OmitByValueExact testType<OmitByValueExact<T, number>>() 1`] = `"Pick<T, { [Key in keyof T]: [number] extends [T[Key]] ? [T[Key]] extends [T[Key] & number] ? never : Key : Key; }[keyof T]>"`;
exports[`Optional testType<Optional<Props, 'age' | 'visible'>>() 1`] = `"(Pick<Props, \\"name\\" | \\"visible\\"> & { age?: number | undefined; }) | (Pick<Props, \\"name\\" | \\"age\\"> & { visible?: boolean | undefined; })"`;
exports[`Optional testType<Optional<Props>>() 1`] = `"Partial<Props>"`;
exports[`OptionalKeys testType<OptionalKeys<RequiredOptionalProps>>() 1`] = `"\\"opt\\" | \\"optUndef\\""`;
exports[`Overwrite const result: Overwrite<Omit<T, 'age'>, T> = rest 1`] = `"any"`;
Expand Down
10 changes: 10 additions & 0 deletions src/mapped-types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
OptionalKeys,
PickByValueExact,
OmitByValueExact,
Optional,
} from './mapped-types';

/**
Expand Down Expand Up @@ -482,3 +483,12 @@ type RequiredOptionalProps = {
// @dts-jest:pass:snap
testType<Brand<number, 'USD'>>();
}

// @dts-jest:group Optional
{
// @dts-jest:pass:snap
testType<Optional<Props>>();

// @dts-jest:pass:snap
testType<Optional<Props, 'age' | 'visible'>>();
}
12 changes: 6 additions & 6 deletions src/mapped-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,16 +561,16 @@ export type Brand<T, U> = T & { __brand: U };
* @example
* type Props = {
* name: string;
* age: string;
* height: number;
* age: number;
* visible: boolean;
* };
*
* // Expect: { name?: string; age?: string; height?: number; }
* // Expect: { name?: string; age?: number; visible?: boolean; }
* type Props = Optional<Props>;
*
* // Expect: { name: string; age?: string; height?: number; }
* type Props = Optional<Props, 'age' | 'height'>;
* // Expect: { name: string; age?: number; visible?: boolean; }
* type Props = Optional<Props, 'age' | 'visible'>;
*/
export type Optional<T extends {}, K = keyof any> = K extends (keyof T)
export type Optional<T extends object, K = keyof any> = K extends (keyof T)
? (Omit<T, K> & { [key in K]?: T[key] })
: Partial<T>;

0 comments on commit f36066b

Please sign in to comment.