From 43ffe03a9b62684b06e12ae61f52e61919aa9008 Mon Sep 17 00:00:00 2001 From: Stepan Burguchev Date: Sun, 9 Jul 2017 14:52:31 +0300 Subject: [PATCH 1/7] enforce strong typing for partial objects and property names --- types/seamless-immutable/index.d.ts | 81 +++++++----- .../seamless-immutable-tests.ts | 123 ++++++++++++------ 2 files changed, 132 insertions(+), 72 deletions(-) diff --git a/types/seamless-immutable/index.d.ts b/types/seamless-immutable/index.d.ts index ceeccd7b3f..046a60259a 100644 --- a/types/seamless-immutable/index.d.ts +++ b/types/seamless-immutable/index.d.ts @@ -15,49 +15,60 @@ export = SeamlessImmutable; declare namespace SeamlessImmutable { - interface MergeConfig { - deep?: boolean; - merger?: Function; - } + type DeepPartial = { + [P in keyof T]?: DeepPartial; + }; - interface Options { - prototype?: any; - } + interface MergeConfig { + deep?: boolean; + merger?: Function; + } - interface AsMutableOptions { - deep: boolean; - } + interface Options { + prototype?: any; + } - export interface ImmutableObject { - set(property: string, value: any): ImmutableObject; - setIn(propertyPath: Array, value: any): ImmutableObject; + interface AsMutableOptions { + deep: boolean; + } - asMutable(): T; - asMutable(opts: AsMutableOptions): T; + export interface ImmutableObject { + set(property: K, value: T[K]): T & ImmutableObject; + setIn + (propertyPath: [ K ], value: T[K]): T & ImmutableObject; + setIn + (propertyPath: [ K, L ], value: T[K][L]): T & ImmutableObject; + setIn + (propertyPath: [ K, L, M ], value: T[K][L][M]): T & ImmutableObject; + setIn + (propertyPath: [ K, L, M, N ], value: T[K][L][M][N]): T & ImmutableObject; - merge(part: any, config?: MergeConfig): ImmutableObject; + asMutable(): T; + asMutable(opts: AsMutableOptions): T; - update(property: string, updaterFunction: (value: any, ...additionalParamters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; - updateIn(propertyPath: Array, updaterFunction: (value: any, ...additionalParamters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; + merge(part: DeepPartial, config?: MergeConfig): T & ImmutableObject; - without(property: string): ImmutableObject; - without(...properties: string[]): ImmutableObject; - without(filter: (value: any, key: string) => boolean): ImmutableObject; - } + update(property: string, updaterFunction: (value: any, ...additionalParamters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; + updateIn(propertyPath: Array, updaterFunction: (value: any, ...additionalParamters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; - export interface ImmutableArray { - asMutable(): Array; - asMutable(opts: AsMutableOptions): Array; - asObject(toKeyValue: (item: T) => Array): ImmutableObject; - flatMap(mapFunction: (item: T) => Array): ImmutableArray; - } + without(property: string): ImmutableObject; + without(...properties: string[]): ImmutableObject; + without(filter: (value: any, key: string) => boolean): ImmutableObject; + } - // an immutable object is both of Type T (i.e., looks like a normal T) and of type Immutable - export type Immutable = T & (ImmutableObject | ImmutableArray); + export interface ImmutableArray { + asMutable(): Array; + asMutable(opts: AsMutableOptions): Array; + asObject(toKeyValue: (item: T) => [string, any]): ImmutableObject; + flatMap(mapFunction: (item: T) => Array): TTarget[] & ImmutableArray; + } - export function from(obj: Array, options?: Options): Array & ImmutableArray; - export function from(obj: T, options?: Options): T & ImmutableObject; + // an immutable object is both of Type T (i.e., looks like a normal T) and of type Immutable + export type Immutable = T & (ImmutableObject | ImmutableArray); - export function isImmutable(target: any): boolean; - export function ImmutableError(message: string): Error; -} \ No newline at end of file + export function from(obj: Array, options?: Options): Array & ImmutableArray; + export function from(obj: T, options?: Options): T & ImmutableObject; + + export function isImmutable(target: any): boolean; + export function ImmutableError(message: string): Error; +} diff --git a/types/seamless-immutable/seamless-immutable-tests.ts b/types/seamless-immutable/seamless-immutable-tests.ts index 745135b6a9..6ab7fd25be 100644 --- a/types/seamless-immutable/seamless-immutable-tests.ts +++ b/types/seamless-immutable/seamless-immutable-tests.ts @@ -1,51 +1,100 @@ -import SI = require("seamless-immutable"); +import * as Immutable from 'seamless-immutable'; -// Immutable instance method test -const isImmutable: boolean = SI.isImmutable(SI.from([0, 2])); -const error: Error = SI.ImmutableError("error"); +// Test types -// Immutable Array tests -const siArray: SI.ImmutableArray = SI.from([0, 1, 2]); - -const mutableArray: Array = siArray.asMutable(); -const mutableArray2: Array = siArray.asMutable({ deep: true }); -const arrayToObject: SI.ImmutableObject = siArray.asObject((value: number) => - [value.toString(), value] -); -const flatMappedArray: SI.ImmutableArray = siArray.flatMap((value: number) => - [value, value] -); - -// Immutable Object tests interface User { - firstName: string; - lastName: string; + firstName: string; + lastName: string; }; interface Address { - line1: string; + line1: string; } interface ExtendedUser extends User { - address: Address; + address: Address; } -const siObject: SI.ImmutableObject = SI.from({ - firstName: "Mike", - lastName: "test" -}); -const extUser: SI.ImmutableObject = siObject.set("address", { - line1: "test" -}); +// +// Constructors +// --------------------------------------------------------------- -const updatedAddress: SI.ImmutableObject = extUser.setIn(["address", "line1"], "test2"); -const mutableUser: ExtendedUser = updatedAddress.asMutable(); -const mutableUser2: ExtendedUser = updatedAddress.asMutable({ deep: true }); -const mergedUser: SI.ImmutableObject = siObject.merge({ lastName: "hello" }); -const updatedUser: SI.ImmutableObject = extUser.update("firstName", (firstName): string => - firstName + "hehe" +{ + interface User { + firstName: string; + lastName: string; + }; + + const arrayOfNumbers: number[] & Immutable.ImmutableArray = Immutable.from([0, 2]); + const user: User & Immutable.ImmutableObject = Immutable.from({ + firstName: 'Angry', + lastName: 'Monkey' + }); + const error: Error = Immutable.ImmutableError('error'); +} + +// +// Static utilities +// --------------------------------------------------------------- + +{ + const isImmutable: boolean = Immutable.isImmutable(Immutable.from([0, 2])); +} + +// +// Instance syntax: immutable array +// --------------------------------------------------------------- +{ + const array: User[] & Immutable.ImmutableArray = Immutable.from([ { firstName: 'Angry', lastName: 'Monkey' } ]); + + // asMutable + const mutableArray1: User[] = array.asMutable(); + const mutableArray2: User[] = array.asMutable({ deep: true }); + + // flatMap + const flatMappedArray: User[] & Immutable.ImmutableArray = array.flatMap((value: User) => + [value, value] + ); + + // asObject + const arrayToObject1: Immutable.ImmutableObject = array.asObject((value) => [value.toString(), value]); +} + +// +// Instance syntax: immutable object +// --------------------------------------------------------------- + +{ + const immutableUser: Immutable.ImmutableObject = Immutable.from({ + firstName: 'Pure', + lastName: 'Gold' + }); + const immutableUserEx: Immutable.ImmutableObject = Immutable.from({ + firstName: 'Hairy', + lastName: 'Dog', + address: { + line1: 'Big house' + } + }); + + // set: property name is strongly checked + const updatedUser: Immutable.ImmutableObject = immutableUser.set('firstName', 'Whirlwind'); + + // setIn: property path is strongly checked for up to 4 arguments + const updatedUser2: Immutable.ImmutableObject = immutableUserEx.setIn(['address', 'line1'], 'Small house'); + + // asMutable + const mutableUser1: User = immutableUser.asMutable(); + const mutableUser2: User = immutableUser.asMutable({ deep: true }); + + // merge: merged part is strongly checked as a deeply partial object + const mergedUser: Immutable.ImmutableObject = immutableUserEx.merge({ address: { line1: 'Small house' }, firstName: 'Jack' }); +} + +const updatedUser: Immutable.ImmutableObject = extUser.update("firstName", (firstName): string => + firstName + "hehe" ); -const updatedInUser: SI.ImmutableObject = extUser.updateIn(["address", "line1"], (line): string => - line + "new address" +const updatedInUser: Immutable.ImmutableObject = extUser.updateIn(["address", "line1"], (line): string => + line + "new address" ); -const userWithoutAddress: SI.ImmutableObject = extUser.without("address"); +const userWithoutAddress: Immutable.ImmutableObject = extUser.without("address"); From 1b91b09021f33ca11d466eb99509b114ebdab5d7 Mon Sep 17 00:00:00 2001 From: Stepan Burguchev Date: Sun, 9 Jul 2017 16:49:49 +0300 Subject: [PATCH 2/7] enforce strong typing in update, updateIn, without --- types/seamless-immutable/index.d.ts | 20 +++++++++------ .../seamless-immutable-tests.ts | 25 +++++++++++-------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/types/seamless-immutable/index.d.ts b/types/seamless-immutable/index.d.ts index 046a60259a..a121e584f1 100644 --- a/types/seamless-immutable/index.d.ts +++ b/types/seamless-immutable/index.d.ts @@ -48,12 +48,19 @@ declare namespace SeamlessImmutable { merge(part: DeepPartial, config?: MergeConfig): T & ImmutableObject; - update(property: string, updaterFunction: (value: any, ...additionalParamters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; - updateIn(propertyPath: Array, updaterFunction: (value: any, ...additionalParamters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; + update(property: K, updaterFunction: (value: T[K], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): T & ImmutableObject; + updateIn + (propertyPath: [ K ], updaterFunction: (value: T[K], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): T & ImmutableObject; + updateIn + (propertyPath: [ K, L ], updaterFunction: (value: T[K][L], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): T & ImmutableObject; + updateIn + (propertyPath: [ K, L, M ], updaterFunction: (value: T[K][L][M], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): T & ImmutableObject; + updateIn + (propertyPath: [ K, L, M, N ], updaterFunction: (value: T[K][L][M][N], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): T & ImmutableObject; - without(property: string): ImmutableObject; - without(...properties: string[]): ImmutableObject; - without(filter: (value: any, key: string) => boolean): ImmutableObject; + without(property: keyof T): TTarget & ImmutableObject; + without(...properties: Array): TTarget & ImmutableObject; + without(filter: (value: T[keyof T], key: keyof T) => boolean): TTarget & ImmutableObject; } export interface ImmutableArray { @@ -63,8 +70,7 @@ declare namespace SeamlessImmutable { flatMap(mapFunction: (item: T) => Array): TTarget[] & ImmutableArray; } - // an immutable object is both of Type T (i.e., looks like a normal T) and of type Immutable - export type Immutable = T & (ImmutableObject | ImmutableArray); + export type Immutable = ImmutableObject | ImmutableArray; export function from(obj: Array, options?: Options): Array & ImmutableArray; export function from(obj: T, options?: Options): T & ImmutableObject; diff --git a/types/seamless-immutable/seamless-immutable-tests.ts b/types/seamless-immutable/seamless-immutable-tests.ts index 6ab7fd25be..e5f7411d55 100644 --- a/types/seamless-immutable/seamless-immutable-tests.ts +++ b/types/seamless-immutable/seamless-immutable-tests.ts @@ -78,23 +78,26 @@ interface ExtendedUser extends User { }); // set: property name is strongly checked - const updatedUser: Immutable.ImmutableObject = immutableUser.set('firstName', 'Whirlwind'); + const updatedUser: User & Immutable.ImmutableObject = immutableUser.set('firstName', 'Whirlwind'); // setIn: property path is strongly checked for up to 4 arguments - const updatedUser2: Immutable.ImmutableObject = immutableUserEx.setIn(['address', 'line1'], 'Small house'); + const updatedUser2: ExtendedUser & Immutable.ImmutableObject = immutableUserEx.setIn(['address', 'line1'], 'Small house'); // asMutable const mutableUser1: User = immutableUser.asMutable(); const mutableUser2: User = immutableUser.asMutable({ deep: true }); // merge: merged part is strongly checked as a deeply partial object - const mergedUser: Immutable.ImmutableObject = immutableUserEx.merge({ address: { line1: 'Small house' }, firstName: 'Jack' }); -} + const mergedUser: User & Immutable.ImmutableObject = immutableUserEx.merge({ address: { line1: 'Small house' }, firstName: 'Jack' }); -const updatedUser: Immutable.ImmutableObject = extUser.update("firstName", (firstName): string => - firstName + "hehe" -); -const updatedInUser: Immutable.ImmutableObject = extUser.updateIn(["address", "line1"], (line): string => - line + "new address" -); -const userWithoutAddress: Immutable.ImmutableObject = extUser.without("address"); + // update: property name is strongly checked + const updatedUser3: User & Immutable.ImmutableObject = immutableUser.update('firstName', x => x.toLowerCase() + ' Whirlwind'); + + // updateIn: property path is strongly checked for up to 4 arguments + const updatedUser4: User & Immutable.ImmutableObject = immutableUserEx.updateIn([ 'address', 'line1' ], x => x.toLowerCase() + ' 43'); + + // without: the return type must be specified explicitly or it will be `any` + const simpleUser1: Immutable.ImmutableObject = immutableUserEx.without('address'); + const simpleUser2: User & Immutable.ImmutableObject = immutableUserEx.without('address'); + const simpleUser3: Immutable.ImmutableObject = immutableUserEx.without('firstName', 'lastName'); +} From fb798e5fabb9cd792dc27c95ae3e963d02964d6d Mon Sep 17 00:00:00 2001 From: Stepan Burguchev Date: Sun, 9 Jul 2017 18:07:06 +0300 Subject: [PATCH 3/7] final polishing --- types/seamless-immutable/index.d.ts | 46 +++++++++++-------- .../seamless-immutable-tests.ts | 24 +++++----- types/seamless-immutable/tsconfig.json | 2 +- 3 files changed, 40 insertions(+), 32 deletions(-) diff --git a/types/seamless-immutable/index.d.ts b/types/seamless-immutable/index.d.ts index a121e584f1..761f6e460c 100644 --- a/types/seamless-immutable/index.d.ts +++ b/types/seamless-immutable/index.d.ts @@ -1,7 +1,9 @@ // Type definitions for Seamless-immutable 6.1.3 // Project: https://github.com/rtfeldman/seamless-immutable // Definitions by: alex3165 +// Stepan Burguchev // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 // This project is licensed under the MIT license. // Copyrights are respective of each contributor listed at the beginning of each definition file. @@ -32,48 +34,54 @@ declare namespace SeamlessImmutable { deep: boolean; } - export interface ImmutableObject { - set(property: K, value: T[K]): T & ImmutableObject; + interface IImmutableObject { + set(property: K, value: T[K]): ImmutableObject; setIn - (propertyPath: [ K ], value: T[K]): T & ImmutableObject; + (propertyPath: [ K ], value: T[K]): ImmutableObject; setIn - (propertyPath: [ K, L ], value: T[K][L]): T & ImmutableObject; + (propertyPath: [ K, L ], value: T[K][L]): ImmutableObject; setIn - (propertyPath: [ K, L, M ], value: T[K][L][M]): T & ImmutableObject; + (propertyPath: [ K, L, M ], value: T[K][L][M]): ImmutableObject; setIn - (propertyPath: [ K, L, M, N ], value: T[K][L][M][N]): T & ImmutableObject; + (propertyPath: [ K, L, M, N ], value: T[K][L][M][N]): ImmutableObject; + setIn + (propertyPath: [ K, L, M, N, O ], value: T[K][L][M][N][O]): ImmutableObject; asMutable(): T; asMutable(opts: AsMutableOptions): T; - merge(part: DeepPartial, config?: MergeConfig): T & ImmutableObject; + merge(part: DeepPartial, config?: MergeConfig): ImmutableObject; - update(property: K, updaterFunction: (value: T[K], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): T & ImmutableObject; + update(property: K, updaterFunction: (value: T[K], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; updateIn - (propertyPath: [ K ], updaterFunction: (value: T[K], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): T & ImmutableObject; + (propertyPath: [ K ], updaterFunction: (value: T[K], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; updateIn - (propertyPath: [ K, L ], updaterFunction: (value: T[K][L], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): T & ImmutableObject; + (propertyPath: [ K, L ], updaterFunction: (value: T[K][L], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; updateIn - (propertyPath: [ K, L, M ], updaterFunction: (value: T[K][L][M], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): T & ImmutableObject; + (propertyPath: [ K, L, M ], updaterFunction: (value: T[K][L][M], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; updateIn - (propertyPath: [ K, L, M, N ], updaterFunction: (value: T[K][L][M][N], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): T & ImmutableObject; + (propertyPath: [ K, L, M, N ], updaterFunction: (value: T[K][L][M][N], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; + updateIn + (propertyPath: [ K, L, M, N, O ], updaterFunction: (value: T[K][L][M][N][O], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; - without(property: keyof T): TTarget & ImmutableObject; - without(...properties: Array): TTarget & ImmutableObject; - without(filter: (value: T[keyof T], key: keyof T) => boolean): TTarget & ImmutableObject; + without(property: keyof T): ImmutableObject; + without(...properties: Array): ImmutableObject; + without(filter: (value: T[keyof T], key: keyof T) => boolean): ImmutableObject; } - export interface ImmutableArray { + interface IImmutableArray { asMutable(): Array; asMutable(opts: AsMutableOptions): Array; asObject(toKeyValue: (item: T) => [string, any]): ImmutableObject; - flatMap(mapFunction: (item: T) => Array): TTarget[] & ImmutableArray; + flatMap(mapFunction: (item: T) => Array): ImmutableArray; } + export type ImmutableObject = T & IImmutableObject; + export type ImmutableArray = Array & IImmutableArray; export type Immutable = ImmutableObject | ImmutableArray; - export function from(obj: Array, options?: Options): Array & ImmutableArray; - export function from(obj: T, options?: Options): T & ImmutableObject; + export function from(obj: Array, options?: Options): ImmutableArray; + export function from(obj: T, options?: Options): ImmutableObject; export function isImmutable(target: any): boolean; export function ImmutableError(message: string): Error; diff --git a/types/seamless-immutable/seamless-immutable-tests.ts b/types/seamless-immutable/seamless-immutable-tests.ts index e5f7411d55..1c5beffad6 100644 --- a/types/seamless-immutable/seamless-immutable-tests.ts +++ b/types/seamless-immutable/seamless-immutable-tests.ts @@ -25,8 +25,8 @@ interface ExtendedUser extends User { lastName: string; }; - const arrayOfNumbers: number[] & Immutable.ImmutableArray = Immutable.from([0, 2]); - const user: User & Immutable.ImmutableObject = Immutable.from({ + const arrayOfNumbers: Immutable.ImmutableArray = Immutable.from([0, 2]); + const user: Immutable.ImmutableObject = Immutable.from({ firstName: 'Angry', lastName: 'Monkey' }); @@ -45,14 +45,14 @@ interface ExtendedUser extends User { // Instance syntax: immutable array // --------------------------------------------------------------- { - const array: User[] & Immutable.ImmutableArray = Immutable.from([ { firstName: 'Angry', lastName: 'Monkey' } ]); + const array: Immutable.ImmutableArray = Immutable.from([ { firstName: 'Angry', lastName: 'Monkey' } ]); // asMutable const mutableArray1: User[] = array.asMutable(); const mutableArray2: User[] = array.asMutable({ deep: true }); // flatMap - const flatMappedArray: User[] & Immutable.ImmutableArray = array.flatMap((value: User) => + const flatMappedArray: Immutable.ImmutableArray = array.flatMap((value: User) => [value, value] ); @@ -78,26 +78,26 @@ interface ExtendedUser extends User { }); // set: property name is strongly checked - const updatedUser: User & Immutable.ImmutableObject = immutableUser.set('firstName', 'Whirlwind'); + const updatedUser: Immutable.ImmutableObject = immutableUser.set('firstName', 'Whirlwind'); - // setIn: property path is strongly checked for up to 4 arguments - const updatedUser2: ExtendedUser & Immutable.ImmutableObject = immutableUserEx.setIn(['address', 'line1'], 'Small house'); + // setIn: property path is strongly checked for up to 5 arguments + const updatedUser2: Immutable.ImmutableObject = immutableUserEx.setIn(['address', 'line1'], 'Small house'); // asMutable const mutableUser1: User = immutableUser.asMutable(); const mutableUser2: User = immutableUser.asMutable({ deep: true }); // merge: merged part is strongly checked as a deeply partial object - const mergedUser: User & Immutable.ImmutableObject = immutableUserEx.merge({ address: { line1: 'Small house' }, firstName: 'Jack' }); + const mergedUser: Immutable.ImmutableObject = immutableUserEx.merge({ address: { line1: 'Small house' }, firstName: 'Jack' }); // update: property name is strongly checked - const updatedUser3: User & Immutable.ImmutableObject = immutableUser.update('firstName', x => x.toLowerCase() + ' Whirlwind'); + const updatedUser3: Immutable.ImmutableObject = immutableUser.update('firstName', x => x.toLowerCase() + ' Whirlwind'); - // updateIn: property path is strongly checked for up to 4 arguments - const updatedUser4: User & Immutable.ImmutableObject = immutableUserEx.updateIn([ 'address', 'line1' ], x => x.toLowerCase() + ' 43'); + // updateIn: property path is strongly checked for up to 5 arguments + const updatedUser4: Immutable.ImmutableObject = immutableUserEx.updateIn([ 'address', 'line1' ], x => x.toLowerCase() + ' 43'); // without: the return type must be specified explicitly or it will be `any` const simpleUser1: Immutable.ImmutableObject = immutableUserEx.without('address'); - const simpleUser2: User & Immutable.ImmutableObject = immutableUserEx.without('address'); + const simpleUser2: Immutable.ImmutableObject = immutableUserEx.without('address'); const simpleUser3: Immutable.ImmutableObject = immutableUserEx.without('firstName', 'lastName'); } diff --git a/types/seamless-immutable/tsconfig.json b/types/seamless-immutable/tsconfig.json index 6ca679e6d8..8e2483cd41 100644 --- a/types/seamless-immutable/tsconfig.json +++ b/types/seamless-immutable/tsconfig.json @@ -19,4 +19,4 @@ "index.d.ts", "seamless-immutable-tests.ts" ] -} \ No newline at end of file +} From ccd26750c8e01d4caa7ef8520666e07458226539 Mon Sep 17 00:00:00 2001 From: Stepan Burguchev Date: Mon, 10 Jul 2017 01:10:45 +0300 Subject: [PATCH 4/7] add fallback definition to solve dynamic scenarios and when the number of arguments is more than 5 --- types/seamless-immutable/index.d.ts | 2 ++ .../seamless-immutable-tests.ts | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/types/seamless-immutable/index.d.ts b/types/seamless-immutable/index.d.ts index 761f6e460c..c027c83429 100644 --- a/types/seamless-immutable/index.d.ts +++ b/types/seamless-immutable/index.d.ts @@ -46,6 +46,7 @@ declare namespace SeamlessImmutable { (propertyPath: [ K, L, M, N ], value: T[K][L][M][N]): ImmutableObject; setIn (propertyPath: [ K, L, M, N, O ], value: T[K][L][M][N][O]): ImmutableObject; + setIn(propertyPath: any[], value: TValue): ImmutableObject; asMutable(): T; asMutable(opts: AsMutableOptions): T; @@ -63,6 +64,7 @@ declare namespace SeamlessImmutable { (propertyPath: [ K, L, M, N ], updaterFunction: (value: T[K][L][M][N], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; updateIn (propertyPath: [ K, L, M, N, O ], updaterFunction: (value: T[K][L][M][N][O], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; + updateIn(propertyPath: any[], updaterFunction: (value: TValue, ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; without(property: keyof T): ImmutableObject; without(...properties: Array): ImmutableObject; diff --git a/types/seamless-immutable/seamless-immutable-tests.ts b/types/seamless-immutable/seamless-immutable-tests.ts index 1c5beffad6..8374056fcc 100644 --- a/types/seamless-immutable/seamless-immutable-tests.ts +++ b/types/seamless-immutable/seamless-immutable-tests.ts @@ -76,12 +76,19 @@ interface ExtendedUser extends User { line1: 'Big house' } }); + const data: { + propertyId: string + } = { + propertyId: 'user.1' + }; // set: property name is strongly checked const updatedUser: Immutable.ImmutableObject = immutableUser.set('firstName', 'Whirlwind'); - // setIn: property path is strongly checked for up to 5 arguments + // setIn: property path is strongly checked for up to 5 arguments (helps with refactoring and intellisense) + // but will fall back to any[] if there are dynamic arguments on the way const updatedUser2: Immutable.ImmutableObject = immutableUserEx.setIn(['address', 'line1'], 'Small house'); + const updatedUser3: Immutable.ImmutableObject = immutableUserEx.setIn([ data.propertyId, 'line1' ], 'Small house'); // asMutable const mutableUser1: User = immutableUser.asMutable(); @@ -91,10 +98,13 @@ interface ExtendedUser extends User { const mergedUser: Immutable.ImmutableObject = immutableUserEx.merge({ address: { line1: 'Small house' }, firstName: 'Jack' }); // update: property name is strongly checked - const updatedUser3: Immutable.ImmutableObject = immutableUser.update('firstName', x => x.toLowerCase() + ' Whirlwind'); + const updatedUser4: Immutable.ImmutableObject = immutableUser.update('firstName', x => x.toLowerCase() + ' Whirlwind'); - // updateIn: property path is strongly checked for up to 5 arguments - const updatedUser4: Immutable.ImmutableObject = immutableUserEx.updateIn([ 'address', 'line1' ], x => x.toLowerCase() + ' 43'); + // updateIn: property path is strongly checked for up to 5 arguments (helps with refactoring and intellisense) + // but will fall back to any[] if there are dynamic arguments on the way + const updatedUser5: Immutable.ImmutableObject = immutableUserEx.updateIn([ 'address', 'line1' ], x => x.toLowerCase() + ' 43'); + // the type of the updated value must be explicity specified in case of fallback + const updatedUser6: Immutable.ImmutableObject = immutableUserEx.updateIn([ data.propertyId, 'line1' ], x => x.toLowerCase() + ' 43'); // without: the return type must be specified explicitly or it will be `any` const simpleUser1: Immutable.ImmutableObject = immutableUserEx.without('address'); From bd54249da7ce63587d342ca046ea819a58349bc1 Mon Sep 17 00:00:00 2001 From: Stepan Burguchev Date: Mon, 10 Jul 2017 09:25:33 +0300 Subject: [PATCH 5/7] set/update fallbacks, tests, tslint coverage --- types/seamless-immutable/index.d.ts | 2 ++ .../seamless-immutable-tests.ts | 23 +++++++++++-------- types/seamless-immutable/tslint.json | 1 + 3 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 types/seamless-immutable/tslint.json diff --git a/types/seamless-immutable/index.d.ts b/types/seamless-immutable/index.d.ts index c027c83429..d40c609dd4 100644 --- a/types/seamless-immutable/index.d.ts +++ b/types/seamless-immutable/index.d.ts @@ -36,6 +36,7 @@ declare namespace SeamlessImmutable { interface IImmutableObject { set(property: K, value: T[K]): ImmutableObject; + set(property: string, value: TValue): ImmutableObject; setIn (propertyPath: [ K ], value: T[K]): ImmutableObject; setIn @@ -54,6 +55,7 @@ declare namespace SeamlessImmutable { merge(part: DeepPartial, config?: MergeConfig): ImmutableObject; update(property: K, updaterFunction: (value: T[K], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; + update(property: string, updaterFunction: (value: TValue, ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; updateIn (propertyPath: [ K ], updaterFunction: (value: T[K], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; updateIn diff --git a/types/seamless-immutable/seamless-immutable-tests.ts b/types/seamless-immutable/seamless-immutable-tests.ts index 8374056fcc..64cbfdd6cb 100644 --- a/types/seamless-immutable/seamless-immutable-tests.ts +++ b/types/seamless-immutable/seamless-immutable-tests.ts @@ -5,7 +5,7 @@ import * as Immutable from 'seamless-immutable'; interface User { firstName: string; lastName: string; -}; +} interface Address { line1: string; @@ -23,7 +23,7 @@ interface ExtendedUser extends User { interface User { firstName: string; lastName: string; - }; + } const arrayOfNumbers: Immutable.ImmutableArray = Immutable.from([0, 2]); const user: Immutable.ImmutableObject = Immutable.from({ @@ -83,28 +83,31 @@ interface ExtendedUser extends User { }; // set: property name is strongly checked - const updatedUser: Immutable.ImmutableObject = immutableUser.set('firstName', 'Whirlwind'); + const updatedUser01: Immutable.ImmutableObject = immutableUser.set('firstName', 'Whirlwind'); + const updatedUser02: Immutable.ImmutableObject = immutableUser.set(data.propertyId, 'Whirlwind'); // setIn: property path is strongly checked for up to 5 arguments (helps with refactoring and intellisense) // but will fall back to any[] if there are dynamic arguments on the way - const updatedUser2: Immutable.ImmutableObject = immutableUserEx.setIn(['address', 'line1'], 'Small house'); - const updatedUser3: Immutable.ImmutableObject = immutableUserEx.setIn([ data.propertyId, 'line1' ], 'Small house'); + const updatedUser11: Immutable.ImmutableObject = immutableUserEx.setIn(['address', 'line1'], 'Small house'); + const updatedUser12: Immutable.ImmutableObject = immutableUserEx.setIn([ data.propertyId, 'line1' ], 'Small house'); // asMutable - const mutableUser1: User = immutableUser.asMutable(); - const mutableUser2: User = immutableUser.asMutable({ deep: true }); + const mutableUser21: User = immutableUser.asMutable(); + const mutableUser22: User = immutableUser.asMutable({ deep: true }); // merge: merged part is strongly checked as a deeply partial object const mergedUser: Immutable.ImmutableObject = immutableUserEx.merge({ address: { line1: 'Small house' }, firstName: 'Jack' }); // update: property name is strongly checked - const updatedUser4: Immutable.ImmutableObject = immutableUser.update('firstName', x => x.toLowerCase() + ' Whirlwind'); + const updatedUser41: Immutable.ImmutableObject = immutableUser.update('firstName', x => x.toLowerCase() + ' Whirlwind'); + // the type of the updated value must be explicity specified in case of fallback + const updatedUser42: Immutable.ImmutableObject = immutableUser.update(data.propertyId, x => x.toLowerCase() + ' Whirlwind'); // updateIn: property path is strongly checked for up to 5 arguments (helps with refactoring and intellisense) // but will fall back to any[] if there are dynamic arguments on the way - const updatedUser5: Immutable.ImmutableObject = immutableUserEx.updateIn([ 'address', 'line1' ], x => x.toLowerCase() + ' 43'); + const updatedUser51: Immutable.ImmutableObject = immutableUserEx.updateIn([ 'address', 'line1' ], x => x.toLowerCase() + ' 43'); // the type of the updated value must be explicity specified in case of fallback - const updatedUser6: Immutable.ImmutableObject = immutableUserEx.updateIn([ data.propertyId, 'line1' ], x => x.toLowerCase() + ' 43'); + const updatedUser52: Immutable.ImmutableObject = immutableUserEx.updateIn([ data.propertyId, 'line1' ], x => x.toLowerCase() + ' 43'); // without: the return type must be specified explicitly or it will be `any` const simpleUser1: Immutable.ImmutableObject = immutableUserEx.without('address'); diff --git a/types/seamless-immutable/tslint.json b/types/seamless-immutable/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/seamless-immutable/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 7730d7619e34ae5b3362d1ba8b3501797c24ea13 Mon Sep 17 00:00:00 2001 From: Stepan Burguchev Date: Mon, 10 Jul 2017 09:49:33 +0300 Subject: [PATCH 6/7] fix common mistakes --- types/seamless-immutable/index.d.ts | 12 +++++++----- types/seamless-immutable/seamless-immutable-tests.ts | 6 ++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/types/seamless-immutable/index.d.ts b/types/seamless-immutable/index.d.ts index d40c609dd4..70aa6b552d 100644 --- a/types/seamless-immutable/index.d.ts +++ b/types/seamless-immutable/index.d.ts @@ -37,6 +37,7 @@ declare namespace SeamlessImmutable { interface IImmutableObject { set(property: K, value: T[K]): ImmutableObject; set(property: string, value: TValue): ImmutableObject; + setIn (propertyPath: [ K ], value: T[K]): ImmutableObject; setIn @@ -47,7 +48,7 @@ declare namespace SeamlessImmutable { (propertyPath: [ K, L, M, N ], value: T[K][L][M][N]): ImmutableObject; setIn (propertyPath: [ K, L, M, N, O ], value: T[K][L][M][N][O]): ImmutableObject; - setIn(propertyPath: any[], value: TValue): ImmutableObject; + setIn(propertyPath: string[], value: TValue): ImmutableObject; asMutable(): T; asMutable(opts: AsMutableOptions): T; @@ -56,6 +57,7 @@ declare namespace SeamlessImmutable { update(property: K, updaterFunction: (value: T[K], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; update(property: string, updaterFunction: (value: TValue, ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; + updateIn (propertyPath: [ K ], updaterFunction: (value: T[K], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; updateIn @@ -66,11 +68,11 @@ declare namespace SeamlessImmutable { (propertyPath: [ K, L, M, N ], updaterFunction: (value: T[K][L][M][N], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; updateIn (propertyPath: [ K, L, M, N, O ], updaterFunction: (value: T[K][L][M][N][O], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; - updateIn(propertyPath: any[], updaterFunction: (value: TValue, ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; + updateIn(propertyPath: string[], updaterFunction: (value: TValue, ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; - without(property: keyof T): ImmutableObject; - without(...properties: Array): ImmutableObject; - without(filter: (value: T[keyof T], key: keyof T) => boolean): ImmutableObject; + without(property: keyof T): ImmutableObject; + without(...properties: Array): ImmutableObject; + without(filter: (value: T[keyof T], key: keyof T) => boolean): ImmutableObject; } interface IImmutableArray { diff --git a/types/seamless-immutable/seamless-immutable-tests.ts b/types/seamless-immutable/seamless-immutable-tests.ts index 64cbfdd6cb..4478ef80cd 100644 --- a/types/seamless-immutable/seamless-immutable-tests.ts +++ b/types/seamless-immutable/seamless-immutable-tests.ts @@ -109,8 +109,6 @@ interface ExtendedUser extends User { // the type of the updated value must be explicity specified in case of fallback const updatedUser52: Immutable.ImmutableObject = immutableUserEx.updateIn([ data.propertyId, 'line1' ], x => x.toLowerCase() + ' 43'); - // without: the return type must be specified explicitly or it will be `any` - const simpleUser1: Immutable.ImmutableObject = immutableUserEx.without('address'); - const simpleUser2: Immutable.ImmutableObject = immutableUserEx.without('address'); - const simpleUser3: Immutable.ImmutableObject = immutableUserEx.without('firstName', 'lastName'); + // without + const simpleUser1: Immutable.ImmutableObject = immutableUserEx.without('address'); } From 4450b36336f984e89f79a97faab6e789cd21cbb0 Mon Sep 17 00:00:00 2001 From: Stepan Burguchev Date: Mon, 10 Jul 2017 10:30:45 +0300 Subject: [PATCH 7/7] add function-like constructor; fix lint errors --- types/seamless-immutable/index.d.ts | 85 ++++++++----------- .../seamless-immutable-tests.ts | 9 +- 2 files changed, 44 insertions(+), 50 deletions(-) diff --git a/types/seamless-immutable/index.d.ts b/types/seamless-immutable/index.d.ts index 70aa6b552d..16256d8e83 100644 --- a/types/seamless-immutable/index.d.ts +++ b/types/seamless-immutable/index.d.ts @@ -1,19 +1,10 @@ -// Type definitions for Seamless-immutable 6.1.3 +// Type definitions for Seamless-immutable 7.1 // Project: https://github.com/rtfeldman/seamless-immutable // Definitions by: alex3165 // Stepan Burguchev // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 -// This project is licensed under the MIT license. -// Copyrights are respective of each contributor listed at the beginning of each definition file. - -// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - export = SeamlessImmutable; declare namespace SeamlessImmutable { @@ -23,7 +14,7 @@ declare namespace SeamlessImmutable { interface MergeConfig { deep?: boolean; - merger?: Function; + merger?(a: any, b: any, config: any): any; } interface Options { @@ -34,61 +25,59 @@ declare namespace SeamlessImmutable { deep: boolean; } - interface IImmutableObject { + interface ImmutableObjectMixin { set(property: K, value: T[K]): ImmutableObject; set(property: string, value: TValue): ImmutableObject; - setIn - (propertyPath: [ K ], value: T[K]): ImmutableObject; - setIn - (propertyPath: [ K, L ], value: T[K][L]): ImmutableObject; - setIn - (propertyPath: [ K, L, M ], value: T[K][L][M]): ImmutableObject; - setIn - (propertyPath: [ K, L, M, N ], value: T[K][L][M][N]): ImmutableObject; - setIn - (propertyPath: [ K, L, M, N, O ], value: T[K][L][M][N][O]): ImmutableObject; + setIn(propertyPath: [ K ], value: T[K]): ImmutableObject; + setIn(propertyPath: [ K, L ], value: T[K][L]): ImmutableObject; + setIn(propertyPath: [ K, L, M ], value: T[K][L][M]): ImmutableObject; + setIn( + propertyPath: [ K, L, M, N ], value: T[K][L][M][N]): ImmutableObject; + setIn( + propertyPath: [ K, L, M, N, O ], value: T[K][L][M][N][O]): ImmutableObject; setIn(propertyPath: string[], value: TValue): ImmutableObject; - asMutable(): T; - asMutable(opts: AsMutableOptions): T; + asMutable(opts?: AsMutableOptions): T; merge(part: DeepPartial, config?: MergeConfig): ImmutableObject; update(property: K, updaterFunction: (value: T[K], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; update(property: string, updaterFunction: (value: TValue, ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; - updateIn - (propertyPath: [ K ], updaterFunction: (value: T[K], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; - updateIn - (propertyPath: [ K, L ], updaterFunction: (value: T[K][L], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; - updateIn - (propertyPath: [ K, L, M ], updaterFunction: (value: T[K][L][M], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; - updateIn - (propertyPath: [ K, L, M, N ], updaterFunction: (value: T[K][L][M][N], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; - updateIn - (propertyPath: [ K, L, M, N, O ], updaterFunction: (value: T[K][L][M][N][O], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; + updateIn( + propertyPath: [ K ], updaterFunction: (value: T[K], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; + updateIn( + propertyPath: [ K, L ], updaterFunction: (value: T[K][L], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; + updateIn( + propertyPath: [ K, L, M ], updaterFunction: (value: T[K][L][M], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; + updateIn( + propertyPath: [ K, L, M, N ], updaterFunction: (value: T[K][L][M][N], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; + updateIn( + propertyPath: [ K, L, M, N, O ], updaterFunction: (value: T[K][L][M][N][O], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; updateIn(propertyPath: string[], updaterFunction: (value: TValue, ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; - without(property: keyof T): ImmutableObject; - without(...properties: Array): ImmutableObject; - without(filter: (value: T[keyof T], key: keyof T) => boolean): ImmutableObject; + without(property: K): ImmutableObject; + without(...properties: K[]): ImmutableObject; + without(filter: (value: T[K], key: K) => boolean): ImmutableObject; } - interface IImmutableArray { - asMutable(): Array; - asMutable(opts: AsMutableOptions): Array; + interface ImmutableArrayMixin { + asMutable(opts?: AsMutableOptions): T[]; asObject(toKeyValue: (item: T) => [string, any]): ImmutableObject; - flatMap(mapFunction: (item: T) => Array): ImmutableArray; + flatMap(mapFunction: (item: T) => TTarget[]): ImmutableArray; } - export type ImmutableObject = T & IImmutableObject; - export type ImmutableArray = Array & IImmutableArray; - export type Immutable = ImmutableObject | ImmutableArray; + type ImmutableObject = T & ImmutableObjectMixin; + type ImmutableArray = T[] & ImmutableArrayMixin; + type Immutable = ImmutableObject | ImmutableArray; - export function from(obj: Array, options?: Options): ImmutableArray; - export function from(obj: T, options?: Options): ImmutableObject; + function from(obj: T[], options?: Options): ImmutableArray; + function from(obj: T, options?: Options): ImmutableObject; - export function isImmutable(target: any): boolean; - export function ImmutableError(message: string): Error; + function isImmutable(target: any): boolean; + function ImmutableError(message: string): Error; } + +declare function SeamlessImmutable(obj: T[], options?: SeamlessImmutable.Options): SeamlessImmutable.ImmutableArray; +declare function SeamlessImmutable(obj: T, options?: SeamlessImmutable.Options): SeamlessImmutable.ImmutableObject; diff --git a/types/seamless-immutable/seamless-immutable-tests.ts b/types/seamless-immutable/seamless-immutable-tests.ts index 4478ef80cd..606d991aea 100644 --- a/types/seamless-immutable/seamless-immutable-tests.ts +++ b/types/seamless-immutable/seamless-immutable-tests.ts @@ -25,8 +25,13 @@ interface ExtendedUser extends User { lastName: string; } - const arrayOfNumbers: Immutable.ImmutableArray = Immutable.from([0, 2]); - const user: Immutable.ImmutableObject = Immutable.from({ + const arrayOfNumbers1: Immutable.ImmutableArray = Immutable.from([0, 2]); + const arrayOfNumbers2: Immutable.ImmutableArray = Immutable([0, 2]); + const user1: Immutable.ImmutableObject = Immutable.from({ + firstName: 'Angry', + lastName: 'Monkey' + }); + const user2: Immutable.ImmutableObject = Immutable({ firstName: 'Angry', lastName: 'Monkey' });