diff --git a/src/impl/objects.ts b/src/impl/objects.ts index 4391cfe..bc92c3f 100644 --- a/src/impl/objects.ts +++ b/src/impl/objects.ts @@ -1,4 +1,4 @@ -import { DeepReadonly, Keys, TaggedObject, ObjectKeys } from '../types/objects'; +import { DeepReadonly, TaggedObject } from '../types/objects'; /** * Type guard for any key, `k`. @@ -17,7 +17,7 @@ export function isKeyOf(obj: T, k: ObjectKeys): k is keyof T { * @returns an array of keys from `obj` */ export function objectKeys(obj: T) { - return Object.keys(obj) as Array>; + return Object.keys(obj) as Array; } /** diff --git a/src/types/objects.ts b/src/types/objects.ts index 3b48b80..fbb41c5 100644 --- a/src/types/objects.ts +++ b/src/types/objects.ts @@ -15,7 +15,7 @@ export type PlainObject = Record; * @returns An object formed by the key, value pairs of T */ export type ObjectType = { - [k in Keys]: T[k]; + [k in keyof T]: T[k]; }; /** * Takes two objects and returns their intersection. @@ -34,7 +34,7 @@ export type CombineObjects = ObjectType = K extends Keys ? T[K] : never; +export type GetKey = K extends keyof T ? T[K] : never; // ---- // Keys @@ -55,13 +55,7 @@ export type ObjectKeys = keyof any; * @param T type from which to get keys * @returns keys of `T` that extend `string` */ -export type StringKeys = Exclude, number | symbol>; -/** - * No different than `keyof`, but can look a bit nicer when nesting many types deep. - * @param T type from which to get keys - * @returns keys of `T` that extend `string | number | symbol` - */ -export type Keys = keyof T; +export type StringKeys = Exclude; /** * When an object has optional or readonly keys, that information is contained within the key. * When using optional/readonly keys in another object, they will retain optional/readonly status. @@ -69,37 +63,37 @@ export type Keys = keyof T; * @param T type from which to get keys * @returns keys of `T` without status modifiers (readonly/optional) */ -export type PureKeys = Record, Keys>[Keys]; +export type PureKeys = Record[keyof T]; /** * Gets all of the keys that are shared between two objects. * @param T first type from which keys will be pulled * @param U second type from which keys will be pulled * @returns the keys that both `T` and `U` have in common. */ -export type SharedKeys = Keys & Keys; +export type SharedKeys = keyof T & keyof U; /** * Gets all keys between two objects. * @param T first type from which keys will be pulled * @param U second type from which keys will be pulled * @returns the keys of `T` in addition to the keys of `U` */ -export type AllKeys = Keys | Keys; +export type AllKeys = keyof T | keyof U; /** * Gets all of the keys that are different between two objects. - * This is a set difference between `Keys` and `Keys`. + * This is a set difference between `keyof T` and `keyof U`. * Note that calling this with arguments reversed will have different results. * @param T first type from which keys will be pulled * @param U second type from which keys will be pulled * @returns keys of `T` minus the keys of `U` */ -export type DiffKeys = Exclude, Keys>; +export type DiffKeys = Exclude; /** * Returns `True` if a key, `K`, is present in a type, `T`, else `False`. * @param T type to check for existence of key `K`. * @param K key to query `T` for * @returns `True` if `K` is a key of `T`. Else `False`. */ -export type HasKey = K extends Keys ? True : False; +export type HasKey = K extends keyof T ? True : False; /** * @param T the union to get the keys of @@ -118,7 +112,7 @@ export type UnionKeys * @param T the object whose property values will be unionized * @returns a union of the right-side values of `T` */ -export type UnionizeProperties = T[Keys]; +export type UnionizeProperties = T[keyof T]; /** * Gives back an object with listed keys removed. * This is the opposite of `Pick`. @@ -126,7 +120,7 @@ export type UnionizeProperties = T[Keys]; * @param K the union of keys to remove from `T` * @returns `T` with the keys `K` removed */ -export type Omit> = Pick, K>>; +export type Omit = Pick>; /** * Returns only the shared properties between two objects. * All shared properties must be the same type. @@ -159,8 +153,8 @@ export type Merge = Overwrite & U; * @param Key the key to add to each inner object as the tag property * @returns a record where each key of the record is now the `Key` property of the inner object */ -export type TaggedObject, Key extends ObjectKeys> = { - [K in Keys]: T[K] & Record; +export type TaggedObject, Key extends keyof any> = { + [K in keyof T]: T[K] & Record; }; // --------- @@ -185,7 +179,7 @@ export type DeepPartial = Partial<{ * @returns `T` with all fields marked required */ export type AllRequired = { - [K in Keys]-?: NonNullable + [K in keyof T]-?: NonNullable }; /** * Mark specific keys, `K`, of `T` as required. @@ -193,7 +187,7 @@ export type AllRequired = { * @param K keys of `T` that will be marked required * @returns `T` with keys, `K`, marked as required */ -export type Required> = CombineObjects< +export type Required = CombineObjects< {[k in K]-?: NonNullable }, Omit >; @@ -203,7 +197,7 @@ export type Required> = CombineObjects< * @param K keys of `T` that will be marked optional * @returns `T` with keys, `K`, marked as optional */ -export type Optional> = CombineObjects< +export type Optional = CombineObjects< {[k in K]?: T[k] | undefined }, Omit >; diff --git a/src/types/predicates.ts b/src/types/predicates.ts index 0a9e871..8c2dac8 100644 --- a/src/types/predicates.ts +++ b/src/types/predicates.ts @@ -1,5 +1,4 @@ import { False, True, And, Or, Not } from './conditionals'; -import { Keys } from './objects'; import { AnyFunc } from './functions'; /** no-doc */ @@ -7,15 +6,15 @@ export type KnownProblemPrototypeKeys = 'toString' | 'toLocaleString' | 'hasOwnP /** no-doc */ export type ArrayPrototypeKeys = keyof unknown[]; /** no-doc */ -export type NumberPrototypeKeys = Keys; +export type NumberPrototypeKeys = keyof number; /** no-doc */ -export type BooleanPrototypeKeys = Keys; +export type BooleanPrototypeKeys = keyof false; /** no-doc */ -export type StringPrototypeKeys = Keys; +export type StringPrototypeKeys = keyof string; /** no-doc */ -export type ObjectPrototypeKeys = Keys; // tslint:disable-line +export type ObjectPrototypeKeys = keyof Object; // tslint:disable-line /** no-doc */ -export type FunctionPrototypeKeys = Keys; // tslint:disable-line +export type FunctionPrototypeKeys = keyof Function; // tslint:disable-line export type IsNever = Not<(Record & Record)[S]>; export type IsType = X extends T ? True : False; diff --git a/test/objects/Keys.test.ts b/test/objects/Keys.test.ts deleted file mode 100644 index 43897c3..0000000 --- a/test/objects/Keys.test.ts +++ /dev/null @@ -1,13 +0,0 @@ -import test from 'ava'; -import { assert } from '../helpers/assert'; - -import { Keys } from '../../src'; - -test('Can get keys from object', t => { - type obj = { x: number, y: string, z: boolean }; - type expected = 'x' | 'y' | 'z'; - type got = Keys; - - assert(t); - assert(t); -});