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

Remove undefined from keyof result of (Pick|Omit)ByValue(Exact)? #124

Merged
merged 2 commits into from
Nov 9, 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
40 changes: 24 additions & 16 deletions src/__snapshots__/mapped-types.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,25 @@ exports[`Omit testType<Omit<Props, 'age'>>() (type) should match snapshot 1`] =

exports[`Omit testType<Omit<T, 'age'>>() (type) should match snapshot 1`] = `"Pick<T, SetDifference<keyof T, \\"age\\">>"`;

exports[`OmitByValue testType<OmitByValue<RequiredOptionalProps, number | undefined>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"opt\\" | \\"optUndef\\" | undefined>"`;
exports[`OmitByValue testType<OmitByValue<RequiredOptionalProps, number | undefined>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"opt\\" | \\"optUndef\\">"`;

exports[`OmitByValue testType<OmitByValue<RequiredOptionalProps, number>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"reqUndef\\" | \\"opt\\" | \\"optUndef\\" | undefined>"`;
exports[`OmitByValue testType<OmitByValue<RequiredOptionalProps, number>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"reqUndef\\" | \\"opt\\" | \\"optUndef\\">"`;

exports[`OmitByValue testType<OmitByValue<RequiredOptionalProps, undefined>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"req\\" | \\"reqUndef\\" | \\"opt\\" | \\"optUndef\\" | undefined>"`;
exports[`OmitByValue testType<OmitByValue<RequiredOptionalProps, undefined>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"req\\" | \\"reqUndef\\" | \\"opt\\" | \\"optUndef\\">"`;

exports[`OmitByValue testType<OmitByValue<T, string | boolean>>() (type) should match snapshot 1`] = `"Pick<T, { [Key in keyof T]: T[Key] extends string | boolean ? never : Key; }[keyof T]>"`;
exports[`OmitByValue testType<OmitByValue<T, string | boolean>>() (type) should match snapshot 1`] = `"Pick<T, { [Key in keyof T]-?: T[Key] extends string | boolean ? never : Key; }[keyof T]>"`;

exports[`OmitByValueExact testType<OmitByValueExact<RequiredOptionalProps, number | undefined>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"req\\" | \\"opt\\" | \\"optUndef\\" | undefined>"`;
exports[`OmitByValue testType<keyof OmitByValue<RequiredOptionalProps, number>>() (type) should match snapshot 1`] = `"\\"reqUndef\\" | \\"opt\\" | \\"optUndef\\""`;

exports[`OmitByValueExact testType<OmitByValueExact<RequiredOptionalProps, number>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"reqUndef\\" | \\"opt\\" | \\"optUndef\\" | undefined>"`;
exports[`OmitByValueExact testType<OmitByValueExact<RequiredOptionalProps, number | undefined>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"req\\" | \\"opt\\" | \\"optUndef\\">"`;

exports[`OmitByValueExact testType<OmitByValueExact<RequiredOptionalProps, undefined>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"req\\" | \\"reqUndef\\" | \\"opt\\" | \\"optUndef\\" | undefined>"`;
exports[`OmitByValueExact testType<OmitByValueExact<RequiredOptionalProps, number>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"reqUndef\\" | \\"opt\\" | \\"optUndef\\">"`;

exports[`OmitByValueExact testType<OmitByValueExact<T, number>>() (type) should match snapshot 1`] = `"Pick<T, { [Key in keyof T]: [number] extends [T[Key]] ? [T[Key]] extends [T[Key] & number] ? never : Key : Key; }[keyof T]>"`;
exports[`OmitByValueExact testType<OmitByValueExact<RequiredOptionalProps, undefined>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"req\\" | \\"reqUndef\\" | \\"opt\\" | \\"optUndef\\">"`;

exports[`OmitByValueExact testType<OmitByValueExact<T, number>>() (type) should match snapshot 1`] = `"Pick<T, { [Key in keyof T]-?: [number] extends [T[Key]] ? [T[Key]] extends [T[Key] & number] ? never : Key : Key; }[keyof T]>"`;

exports[`OmitByValueExact testType<keyof OmitByValueExact<RequiredOptionalProps, number>>() (type) should match snapshot 1`] = `"\\"reqUndef\\" | \\"opt\\" | \\"optUndef\\""`;

exports[`Optional testType<Optional<Props, 'age' | 'visible'>>({ name: 'Yolo' }) (type) should match snapshot 1`] = `"Optional<Props, \\"age\\" | \\"visible\\">"`;

Expand All @@ -169,21 +173,25 @@ exports[`Overwrite const result: Overwrite<Omit<T, 'age'>, T> = rest (type) shou

exports[`Overwrite testType<Overwrite<Props, NewProps>>() (type) should match snapshot 1`] = `"Pick<Pick<Props, \\"name\\" | \\"visible\\"> & Pick<NewProps, \\"age\\">, \\"name\\" | \\"age\\" | \\"visible\\">"`;

exports[`PickByValue testType<PickByValue<RequiredOptionalProps, number | undefined>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"req\\" | \\"reqUndef\\" | undefined>"`;
exports[`PickByValue testType<PickByValue<RequiredOptionalProps, number | undefined>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"req\\" | \\"reqUndef\\">"`;

exports[`PickByValue testType<PickByValue<RequiredOptionalProps, number>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"req\\">"`;

exports[`PickByValue testType<PickByValue<RequiredOptionalProps, undefined>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, never>"`;

exports[`PickByValue testType<PickByValue<RequiredOptionalProps, number>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"req\\" | undefined>"`;
exports[`PickByValue testType<PickByValue<T, number>>() (type) should match snapshot 1`] = `"Pick<T, { [Key in keyof T]-?: T[Key] extends number ? Key : never; }[keyof T]>"`;

exports[`PickByValue testType<PickByValue<RequiredOptionalProps, undefined>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, undefined>"`;
exports[`PickByValue testType<keyof PickByValue<RequiredOptionalProps, number>>() (type) should match snapshot 1`] = `"\\"req\\""`;

exports[`PickByValue testType<PickByValue<T, number>>() (type) should match snapshot 1`] = `"Pick<T, { [Key in keyof T]: T[Key] extends number ? Key : never; }[keyof T]>"`;
exports[`PickByValueExact testType<PickByValueExact<RequiredOptionalProps, number | undefined>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"reqUndef\\">"`;

exports[`PickByValueExact testType<PickByValueExact<RequiredOptionalProps, number | undefined>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"reqUndef\\" | undefined>"`;
exports[`PickByValueExact testType<PickByValueExact<RequiredOptionalProps, number>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"req\\">"`;

exports[`PickByValueExact testType<PickByValueExact<RequiredOptionalProps, number>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, \\"req\\" | undefined>"`;
exports[`PickByValueExact testType<PickByValueExact<RequiredOptionalProps, undefined>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, never>"`;

exports[`PickByValueExact testType<PickByValueExact<RequiredOptionalProps, undefined>>() (type) should match snapshot 1`] = `"Pick<RequiredOptionalProps, undefined>"`;
exports[`PickByValueExact testType<PickByValueExact<T, number>>() (type) should match snapshot 1`] = `"Pick<T, { [Key in keyof T]-?: [number] extends [T[Key]] ? [T[Key]] extends [T[Key] & number] ? Key : never : never; }[keyof T]>"`;

exports[`PickByValueExact testType<PickByValueExact<T, number>>() (type) should match snapshot 1`] = `"Pick<T, { [Key in keyof T]: [number] extends [T[Key]] ? [T[Key]] extends [T[Key] & number] ? Key : never : never; }[keyof T]>"`;
exports[`PickByValueExact testType<keyof PickByValueExact<RequiredOptionalProps, number>>() (type) should match snapshot 1`] = `"\\"req\\""`;

exports[`PromiseType testType<PromiseType<Promise<string>>>() (type) should match snapshot 1`] = `"string"`;

Expand Down
40 changes: 24 additions & 16 deletions src/mapped-types.spec.snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,30 +142,34 @@ type RequiredOptionalProps = {

// @dts-jest:group PickByValue
{
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "req" | undefined>
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "req">
testType<PickByValue<RequiredOptionalProps, number>>();
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "req" | "reqUndef" | undefined>
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "req" | "reqUndef">
testType<PickByValue<RequiredOptionalProps, number | undefined>>();
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, undefined>
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, never>
testType<PickByValue<RequiredOptionalProps, undefined>>();
// @dts-jest:pass:snap -> "req"
testType<keyof PickByValue<RequiredOptionalProps, number>>();

const fn = <T extends Props>(props: T) => {
// @dts-jest:pass:snap -> Pick<T, { [Key in keyof T]: T[Key] extends number ? Key : never; }[keyof T]>
// @dts-jest:pass:snap -> Pick<T, { [Key in keyof T]-?: T[Key] extends number ? Key : never; }[keyof T]>
testType<PickByValue<T, number>>();
};
}

// @dts-jest:group PickByValueExact
{
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "req" | undefined>
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "req">
testType<PickByValueExact<RequiredOptionalProps, number>>();
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "reqUndef" | undefined>
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "reqUndef">
testType<PickByValueExact<RequiredOptionalProps, number | undefined>>();
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, undefined>
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, never>
testType<PickByValueExact<RequiredOptionalProps, undefined>>();
// @dts-jest:pass:snap -> "req"
testType<keyof PickByValueExact<RequiredOptionalProps, number>>();

const fn = <T extends Props>(props: T) => {
// @dts-jest:pass:snap -> Pick<T, { [Key in keyof T]: [number] extends [T[Key]] ? [T[Key]] extends [T[Key] & number] ? Key : never : never; }[keyof T]>
// @dts-jest:pass:snap -> Pick<T, { [Key in keyof T]-?: [number] extends [T[Key]] ? [T[Key]] extends [T[Key] & number] ? Key : never : never; }[keyof T]>
testType<PickByValueExact<T, number>>();
};
}
Expand All @@ -189,30 +193,34 @@ type RequiredOptionalProps = {

// @dts-jest:group OmitByValue
{
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "reqUndef" | "opt" | "optUndef" | undefined>
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "reqUndef" | "opt" | "optUndef">
testType<OmitByValue<RequiredOptionalProps, number>>();
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "opt" | "optUndef" | undefined>
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "opt" | "optUndef">
testType<OmitByValue<RequiredOptionalProps, number | undefined>>();
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "req" | "reqUndef" | "opt" | "optUndef" | undefined>
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "req" | "reqUndef" | "opt" | "optUndef">
testType<OmitByValue<RequiredOptionalProps, undefined>>();
// @dts-jest:pass:snap -> "reqUndef" | "opt" | "optUndef"
testType<keyof OmitByValue<RequiredOptionalProps, number>>();

const fn = <T extends Props>(props: T) => {
// @dts-jest:pass:snap -> Pick<T, { [Key in keyof T]: T[Key] extends string | boolean ? never : Key; }[keyof T]>
// @dts-jest:pass:snap -> Pick<T, { [Key in keyof T]-?: T[Key] extends string | boolean ? never : Key; }[keyof T]>
testType<OmitByValue<T, string | boolean>>();
};
}

// @dts-jest:group OmitByValueExact
{
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "reqUndef" | "opt" | "optUndef" | undefined>
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "reqUndef" | "opt" | "optUndef">
testType<OmitByValueExact<RequiredOptionalProps, number>>();
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "req" | "opt" | "optUndef" | undefined>
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "req" | "opt" | "optUndef">
testType<OmitByValueExact<RequiredOptionalProps, number | undefined>>();
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "req" | "reqUndef" | "opt" | "optUndef" | undefined>
// @dts-jest:pass:snap -> Pick<RequiredOptionalProps, "req" | "reqUndef" | "opt" | "optUndef">
testType<OmitByValueExact<RequiredOptionalProps, undefined>>();
// @dts-jest:pass:snap -> "reqUndef" | "opt" | "optUndef"
testType<keyof OmitByValueExact<RequiredOptionalProps, number>>();

const fn = <T extends Props>(props: T) => {
// @dts-jest:pass:snap -> Pick<T, { [Key in keyof T]: [number] extends [T[Key]] ? [T[Key]] extends [T[Key] & number] ? never : Key : Key; }[keyof T]>
// @dts-jest:pass:snap -> Pick<T, { [Key in keyof T]-?: [number] extends [T[Key]] ? [T[Key]] extends [T[Key] & number] ? never : Key : Key; }[keyof T]>
testType<OmitByValueExact<T, number>>();
};
}
Expand Down
8 changes: 8 additions & 0 deletions src/mapped-types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ type RequiredOptionalProps = {
testType<PickByValue<RequiredOptionalProps, number | undefined>>();
// @dts-jest:pass:snap
testType<PickByValue<RequiredOptionalProps, undefined>>();
// @dts-jest:pass:snap
testType<keyof PickByValue<RequiredOptionalProps, number>>();

const fn = <T extends Props>(props: T) => {
// @dts-jest:pass:snap
Expand All @@ -163,6 +165,8 @@ type RequiredOptionalProps = {
testType<PickByValueExact<RequiredOptionalProps, number | undefined>>();
// @dts-jest:pass:snap
testType<PickByValueExact<RequiredOptionalProps, undefined>>();
// @dts-jest:pass:snap
testType<keyof PickByValueExact<RequiredOptionalProps, number>>();

const fn = <T extends Props>(props: T) => {
// @dts-jest:pass:snap
Expand Down Expand Up @@ -195,6 +199,8 @@ type RequiredOptionalProps = {
testType<OmitByValue<RequiredOptionalProps, number | undefined>>();
// @dts-jest:pass:snap
testType<OmitByValue<RequiredOptionalProps, undefined>>();
// @dts-jest:pass:snap
testType<keyof OmitByValue<RequiredOptionalProps, number>>();

const fn = <T extends Props>(props: T) => {
// @dts-jest:pass:snap
Expand All @@ -210,6 +216,8 @@ type RequiredOptionalProps = {
testType<OmitByValueExact<RequiredOptionalProps, number | undefined>>();
// @dts-jest:pass:snap
testType<OmitByValueExact<RequiredOptionalProps, undefined>>();
// @dts-jest:pass:snap
testType<keyof OmitByValueExact<RequiredOptionalProps, number>>();

const fn = <T extends Props>(props: T) => {
// @dts-jest:pass:snap
Expand Down
8 changes: 4 additions & 4 deletions src/mapped-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ namespace Pick {}
*/
export type PickByValue<T, ValueType> = Pick<
T,
{ [Key in keyof T]: T[Key] extends ValueType ? Key : never }[keyof T]
{ [Key in keyof T]-?: T[Key] extends ValueType ? Key : never }[keyof T]
>;

/**
Expand All @@ -207,7 +207,7 @@ export type PickByValue<T, ValueType> = Pick<
export type PickByValueExact<T, ValueType> = Pick<
T,
{
[Key in keyof T]: [ValueType] extends [T[Key]]
[Key in keyof T]-?: [ValueType] extends [T[Key]]
? [T[Key]] extends [ValueType]
? Key
: never
Expand Down Expand Up @@ -240,7 +240,7 @@ export type Omit<T, K extends keyof any> = Pick<T, SetDifference<keyof T, K>>;
*/
export type OmitByValue<T, ValueType> = Pick<
T,
{ [Key in keyof T]: T[Key] extends ValueType ? never : Key }[keyof T]
{ [Key in keyof T]-?: T[Key] extends ValueType ? never : Key }[keyof T]
>;

/**
Expand All @@ -257,7 +257,7 @@ export type OmitByValue<T, ValueType> = Pick<
export type OmitByValueExact<T, ValueType> = Pick<
T,
{
[Key in keyof T]: [ValueType] extends [T[Key]]
[Key in keyof T]-?: [ValueType] extends [T[Key]]
? [T[Key]] extends [ValueType]
? never
: Key
Expand Down