Skip to content
This repository has been archived by the owner on Mar 20, 2022. It is now read-only.

Commit

Permalink
[enhance] Improve typescript types with generics (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker authored and paularmstrong committed Aug 5, 2019
1 parent 88a25c5 commit 15f6dfe
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions index.d.ts
@@ -1,59 +1,62 @@
declare namespace schema {
export type StrategyFunction = (value: any, parent: any, key: string) => any;
export type StrategyFunction<T> = (value: any, parent: any, key: string) => T;
export type SchemaFunction = (value: any, parent: any, key: string) => string;
export type MergeFunction = (entityA: any, entityB: any) => any;

export class Array {
constructor(definition: Schema, schemaAttribute?: string | SchemaFunction)
export class Array<T = any> {
constructor(definition: Schema<T>, schemaAttribute?: string | SchemaFunction)
define(definition: Schema): void
}

export interface EntityOptions {
export interface EntityOptions<T = any> {
idAttribute?: string | SchemaFunction
mergeStrategy?: MergeFunction
processStrategy?: StrategyFunction
processStrategy?: StrategyFunction<T>
}

export class Entity {
constructor(key: string | symbol, definition?: Schema, options?: EntityOptions)
export class Entity<T = any> {
constructor(key: string | symbol, definition?: Schema, options?: EntityOptions<T>)
define(definition: Schema): void
key: string
getId: SchemaFunction
_processStrategy: StrategyFunction<T>
}

export class Object {
constructor(definition: {[key: string]: Schema})
export class Object<T = any> {
constructor(definition: {[key: string]: Schema<T>})
define(definition: Schema): void
}

export class Union {
constructor(definition: Schema, schemaAttribute?: string | SchemaFunction)
export class Union<T = any> {
constructor(definition: Schema<T>, schemaAttribute?: string | SchemaFunction)
define(definition: Schema): void
}

export class Values {
constructor(definition: Schema, schemaAttribute?: string | SchemaFunction)
export class Values<T = any> {
constructor(definition: Schema<T>, schemaAttribute?: string | SchemaFunction)
define(definition: Schema): void
}
}

export type Schema =
schema.Array |
schema.Entity |
schema.Object |
schema.Union |
schema.Values |
schema.Array[] |
schema.Entity[] |
schema.Object[] |
schema.Union[] |
schema.Values[] |
{[key: string]: Schema | Schema[]};
export type Schema<T = any> =
| schema.Entity<T>
| schema.Object<T>
| schema.Union<T>
| schema.Values<T>
| SchemaObject<T>
| SchemaArray<T>;

export interface SchemaObject<T> {
[key: string]: Schema<T>
}

export interface SchemaArray<T> extends Array<Schema<T>> {}

export type NormalizedSchema<E, R> = { entities: E, result: R };

export function normalize<E = any, R = any>(
export function normalize<T = any, E = { [key:string]: { [key:string]: T }}, R = any>(
data: any,
schema: Schema
schema: Schema<T>
): NormalizedSchema<E, R>;

export function denormalize(
Expand Down

0 comments on commit 15f6dfe

Please sign in to comment.