fix: Fix includeKeys return type when passing a union of objects#39
Open
gmaclennan wants to merge 1 commit intosindresorhus:mainfrom
Open
fix: Fix includeKeys return type when passing a union of objects#39gmaclennan wants to merge 1 commit intosindresorhus:mainfrom
gmaclennan wants to merge 1 commit intosindresorhus:mainfrom
Conversation
Use a [distributive conditional type](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types) that returns the correct type when using `includeKeys` on a union type.
Owner
|
AI review: Problems in the PR:
Minimal fix (types only): // Internal helpers
type DistributivePick<Value, Key extends PropertyKey> =
Value extends unknown ? Pick<Value, Extract<Key, keyof Value>> : never;
type DistributiveOmit<Value, Key extends PropertyKey> =
Value extends unknown ? Omit<Value, Extract<Key, keyof Value>> : never;
// includeKeys
export function includeKeys<
ObjectType extends Record<PropertyKey, unknown>,
IncludedKeys extends PropertyKey
>(
object: ObjectType,
keys: readonly IncludedKeys[] | ReadonlySet<IncludedKeys>
): DistributivePick<ObjectType, IncludedKeys>;
// excludeKeys
export function excludeKeys<
ObjectType extends Record<PropertyKey, unknown>,
ExcludedKeys extends PropertyKey
>(
object: ObjectType,
keys: readonly ExcludedKeys[] | ReadonlySet<ExcludedKeys>
): DistributiveOmit<ObjectType, ExcludedKeys>;Add tests that actually fail without type U =
| { kind: 'a'; a: number }
| { kind: 'b'; b: boolean }
| { kind: 'c'; c: bigint };
declare const u: U;
// include keys present on some arms only
expectType<{ a: number } | {} | {}>(includeKeys(u, ['a'] as const));
expectType<{ b: boolean } | {} | {}>(includeKeys(u, ['b'] as const));
expectType<{ a: number } | { b: boolean } | {}>(includeKeys(u, ['a', 'b'] as const));
// parity for exclude
expectType<{ kind: 'a' } | { b: boolean; kind: 'b' } | { c: bigint; kind: 'c' }>(
excludeKeys(
({ kind: 'a', a: 1 } as U),
['a', 'nonExistent'] as const
)
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Similar to #34, this uses a distributive conditional type that returns the correct type when using
includeKeyson a union type.I'm not sure if there is a strange edge-case that means this does not work as-expected for
includeKeys, I don't want to break types for regular use.