Skip to content

Commit

Permalink
Improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
papb committed Sep 16, 2020
1 parent a7b9c7e commit 035edb0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
9 changes: 4 additions & 5 deletions source/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isPlainObject } from './is-plain-object';
import { isPlainObject, AnyPlainObject } from './is-plain-object';

type AnyObject = Record<string, unknown>;
type AnyArray = readonly any[];
type PropOrNever<O, Prop> = Prop extends string | number ? (O extends Record<Prop, any> ? O[Prop] : never) : never;
type PropOrUndef<O, Prop extends string> = O extends Record<Prop, any> ? O[Prop] : undefined;
Expand All @@ -23,8 +22,8 @@ export type TreeShortcut<
? PropOrUndefEnteringArrayIfNeeded<T[PropName], InnerPropName>
: TreeShortcut<PropOrNever<T, K>, PropName, InnerPropName, ShortcutName>;
}
: T extends AnyArray | AnyObject
// Due to the distribution above, here T will not be `AnyArray | AnyObject`,
: T extends AnyArray | AnyPlainObject
// Due to the distribution above, here T will not be `AnyArray | AnyPlainObject`,
// since if it were, it would have distributed. This is important since
// `keyof ([1] | { a: 1 })` is `never`, while we want a distribution over `0` and `'a'`
? { [K in keyof T]: TreeShortcut<T[K], PropName, InnerPropName, ShortcutName> }
Expand Down Expand Up @@ -58,7 +57,7 @@ function treeShortcutHelper(tree: any, from: string, to: string, name: string):
}

export function treeShortcut<
Tree,
Tree extends AnyArray | AnyPlainObject,
ShortcutTriggerProp extends string,
ShortcutTargetProp extends string,
ShortcutName extends string,
Expand Down
4 changes: 3 additions & 1 deletion source/is-plain-object.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import isPlainObject_ = require('is-plain-obj');

export function isPlainObject(value: any): value is Record<string | number | symbol, unknown> {
export type AnyPlainObject = Record<string | number | symbol, unknown>;

export function isPlainObject(value: any): value is AnyPlainObject {
return isPlainObject_(value);
}

0 comments on commit 035edb0

Please sign in to comment.