Skip to content

Commit

Permalink
Merge pull request #74 from andnp/ObjectKeys
Browse files Browse the repository at this point in the history
Preempt future typescript breaking change.
  • Loading branch information
andnp authored Oct 28, 2018
2 parents 45a4d71 + 069da08 commit 96320a2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ npm install --save-dev simplytyped

**[Objects](#objects)**

[AllKeys](#allkeys) - [AllRequired](#allrequired) - [CombineObjects](#combineobjects) - [DeepPartial](#deeppartial) - [DeepReadonly](#deepreadonly) - [DiffKeys](#diffkeys) - [GetKey](#getkey) - [HasKey](#haskey) - [Intersect](#intersect) - [Keys](#keys) - [KeysByType](#keysbytype) - [Merge](#merge) - [ObjectType](#objecttype) - [Omit](#omit) - [Optional](#optional) - [Overwrite](#overwrite) - [PlainObject](#plainobject) - [PureKeys](#purekeys) - [Required](#required) - [SharedKeys](#sharedkeys) - [StringKeys](#stringkeys) - [TaggedObject](#taggedobject) - [UnionizeProperties](#unionizeproperties)
[AllKeys](#allkeys) - [AllRequired](#allrequired) - [CombineObjects](#combineobjects) - [DeepPartial](#deeppartial) - [DeepReadonly](#deepreadonly) - [DiffKeys](#diffkeys) - [GetKey](#getkey) - [HasKey](#haskey) - [Intersect](#intersect) - [Keys](#keys) - [KeysByType](#keysbytype) - [Merge](#merge) - [ObjectKeys](#objectkeys) - [ObjectType](#objecttype) - [Omit](#omit) - [Optional](#optional) - [Overwrite](#overwrite) - [PlainObject](#plainobject) - [PureKeys](#purekeys) - [Required](#required) - [SharedKeys](#sharedkeys) - [StringKeys](#stringkeys) - [TaggedObject](#taggedobject) - [UnionizeProperties](#unionizeproperties)

**[Utils](#utils)**

Expand Down Expand Up @@ -346,6 +346,15 @@ test('Can merge an object containing all strings as keys', t => {

```

### ObjectKeys
Objects can be indexed by multiple types: `string`, `number`, `symbol`.
For safe compatibility with typescript version, this type will always
have the correct set of object key types for the current version of TS.

This is useful for functions that must take a key, instead of `K extends string`,
use `K extends ObjectKeys`.


### ObjectType
Takes any type and makes it an object type.
Useful when combined with `&` intersection types.
Expand Down
8 changes: 6 additions & 2 deletions scripts/generateDocumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,12 @@ console.log(markdown);


/** True if this is visible outside this file, false otherwise */
function isNodeExported(node: tsc.Node): boolean {
return (tsc.getCombinedModifierFlags(node) & tsc.ModifierFlags.Export) !== 0 || (!!node.parent && node.parent.kind === tsc.SyntaxKind.SourceFile); // tslint:disable-line no-bitwise
function isNodeExported(node: tsc.Node | tsc.Declaration): boolean {
const declaration: tsc.Declaration = {
_declarationBrand: 'branded',
...node,
};
return (tsc.getCombinedModifierFlags(declaration) & tsc.ModifierFlags.Export) !== 0 || (!!declaration.parent && declaration.parent.kind === tsc.SyntaxKind.SourceFile); // tslint:disable-line no-bitwise
}

// TODO: make this less atrocious
Expand Down
4 changes: 2 additions & 2 deletions src/impl/objects.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DeepReadonly, Keys, TaggedObject } from '../types/objects';
import { DeepReadonly, Keys, TaggedObject, ObjectKeys } from '../types/objects';

/**
* Type guard for any key, `k`.
Expand Down Expand Up @@ -34,7 +34,7 @@ export function Readonly<T extends object>(obj: T): DeepReadonly<T> { return obj
* @param key the name of the "tag" parameter
* @returns `obj` with the inner objects tagged with parameter `key` and the key pointing to that inner object
*/
export function taggedObject<T extends Record<string, object>, K extends string>(obj: T, key: K): TaggedObject<T, K> {
export function taggedObject<T extends Record<ObjectKeys, object>, K extends string>(obj: T, key: K): TaggedObject<T, K> {
const keys = objectKeys(obj);
return keys.reduce((collection: any, k) => {
const inner: any = obj[k];
Expand Down
9 changes: 9 additions & 0 deletions src/types/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ export type GetKey<T, K extends keyof any> = K extends Keys<T> ? T[K] : never;
// ----
// Keys
// ----
/**
* Objects can be indexed by multiple types: `string`, `number`, `symbol`.
* For safe compatibility with typescript version, this type will always
* have the correct set of object key types for the current version of TS.
*
* This is useful for functions that must take a key, instead of `K extends string`,
* use `K extends ObjectKeys`.
*/
export type ObjectKeys = keyof any;
/**
* Typescript 2.9 introduced `number | symbol` as possible results from `keyof any`.
* For backwards compatibility with objects containing only `string` keys, this will
Expand Down

0 comments on commit 96320a2

Please sign in to comment.