From 15f6dfea856ecfd5f109a2f5ad49cb6aa6467011 Mon Sep 17 00:00:00 2001 From: Nathaniel Tucker Date: Mon, 5 Aug 2019 08:37:56 -0700 Subject: [PATCH] [enhance] Improve typescript types with generics (#365) --- index.d.ts | 57 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/index.d.ts b/index.d.ts index c9c4c49d..6713dc52 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,59 +1,62 @@ declare namespace schema { - export type StrategyFunction = (value: any, parent: any, key: string) => any; + export type StrategyFunction = (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 { + constructor(definition: Schema, schemaAttribute?: string | SchemaFunction) define(definition: Schema): void } - export interface EntityOptions { + export interface EntityOptions { idAttribute?: string | SchemaFunction mergeStrategy?: MergeFunction - processStrategy?: StrategyFunction + processStrategy?: StrategyFunction } - export class Entity { - constructor(key: string | symbol, definition?: Schema, options?: EntityOptions) + export class Entity { + constructor(key: string | symbol, definition?: Schema, options?: EntityOptions) define(definition: Schema): void key: string + getId: SchemaFunction + _processStrategy: StrategyFunction } - export class Object { - constructor(definition: {[key: string]: Schema}) + export class Object { + constructor(definition: {[key: string]: Schema}) define(definition: Schema): void } - export class Union { - constructor(definition: Schema, schemaAttribute?: string | SchemaFunction) + export class Union { + constructor(definition: Schema, schemaAttribute?: string | SchemaFunction) define(definition: Schema): void } - export class Values { - constructor(definition: Schema, schemaAttribute?: string | SchemaFunction) + export class Values { + constructor(definition: Schema, 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 = + | schema.Entity + | schema.Object + | schema.Union + | schema.Values + | SchemaObject + | SchemaArray; + +export interface SchemaObject { + [key: string]: Schema +} + +export interface SchemaArray extends Array> {} export type NormalizedSchema = { entities: E, result: R }; -export function normalize( +export function normalize( data: any, - schema: Schema + schema: Schema ): NormalizedSchema; export function denormalize(