diff --git a/types/seamless-immutable/index.d.ts b/types/seamless-immutable/index.d.ts index ceeccd7b3f..16256d8e83 100644 --- a/types/seamless-immutable/index.d.ts +++ b/types/seamless-immutable/index.d.ts @@ -1,63 +1,83 @@ -// 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 - -// 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. +// TypeScript Version: 2.3 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?(a: any, b: any, config: any): any; + } - 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; + interface ImmutableObjectMixin { + set(property: K, value: T[K]): ImmutableObject; + set(property: string, value: TValue): ImmutableObject; - merge(part: any, config?: MergeConfig): 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; - update(property: string, updaterFunction: (value: any, ...additionalParamters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; - updateIn(propertyPath: Array, updaterFunction: (value: any, ...additionalParamters: any[]) => any, ...additionalArguments: any[]): ImmutableObject; + asMutable(opts?: AsMutableOptions): T; - without(property: string): ImmutableObject; - without(...properties: string[]): ImmutableObject; - without(filter: (value: any, key: string) => boolean): ImmutableObject; - } + merge(part: DeepPartial, config?: MergeConfig): ImmutableObject; - export interface ImmutableArray { - asMutable(): Array; - asMutable(opts: AsMutableOptions): Array; - asObject(toKeyValue: (item: T) => Array): ImmutableObject; - flatMap(mapFunction: (item: T) => Array): ImmutableArray; - } + 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; - // 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); + 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; - export function from(obj: Array, options?: Options): Array & ImmutableArray; - export function from(obj: T, options?: Options): T & ImmutableObject; + without(property: K): ImmutableObject; + without(...properties: K[]): ImmutableObject; + without(filter: (value: T[K], key: K) => boolean): ImmutableObject; + } - export function isImmutable(target: any): boolean; - export function ImmutableError(message: string): Error; -} \ No newline at end of file + interface ImmutableArrayMixin { + asMutable(opts?: AsMutableOptions): T[]; + asObject(toKeyValue: (item: T) => [string, any]): ImmutableObject; + flatMap(mapFunction: (item: T) => TTarget[]): ImmutableArray; + } + + type ImmutableObject = T & ImmutableObjectMixin; + type ImmutableArray = T[] & ImmutableArrayMixin; + type Immutable = ImmutableObject | ImmutableArray; + + function from(obj: T[], options?: Options): ImmutableArray; + function from(obj: T, options?: Options): ImmutableObject; + + 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 745135b6a9..606d991aea 100644 --- a/types/seamless-immutable/seamless-immutable-tests.ts +++ b/types/seamless-immutable/seamless-immutable-tests.ts @@ -1,51 +1,119 @@ -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" -); -const updatedInUser: SI.ImmutableObject = extUser.updateIn(["address", "line1"], (line): string => - line + "new address" -); -const userWithoutAddress: SI.ImmutableObject = extUser.without("address"); +{ + interface User { + firstName: string; + lastName: string; + } + + 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' + }); + const error: Error = Immutable.ImmutableError('error'); +} + +// +// Static utilities +// --------------------------------------------------------------- + +{ + const isImmutable: boolean = Immutable.isImmutable(Immutable.from([0, 2])); +} + +// +// Instance syntax: immutable array +// --------------------------------------------------------------- +{ + 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: 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' + } + }); + const data: { + propertyId: string + } = { + propertyId: 'user.1' + }; + + // set: property name is strongly checked + 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 updatedUser11: Immutable.ImmutableObject = immutableUserEx.setIn(['address', 'line1'], 'Small house'); + const updatedUser12: Immutable.ImmutableObject = immutableUserEx.setIn([ data.propertyId, 'line1' ], 'Small house'); + + // asMutable + 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 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 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 updatedUser52: Immutable.ImmutableObject = immutableUserEx.updateIn([ data.propertyId, 'line1' ], x => x.toLowerCase() + ' 43'); + + // without + const simpleUser1: Immutable.ImmutableObject = immutableUserEx.without('address'); +} 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 +} 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" }