Merge pull request #17901 from xsburg/master

[seamless-immutable] replace `any` with meaningful types, enforce string property typing, fix bugs
This commit is contained in:
Ryan Cavanaugh 2017-07-10 10:25:49 -07:00 committed by GitHub
commit 6cfb7b72f8
4 changed files with 175 additions and 86 deletions

View File

@ -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 <https://github.com/alex3165>
// Stepan Burguchev <https://github.com/xsburg>
// 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<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};
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<T> {
set(property: string, value: any): ImmutableObject<any>;
setIn(propertyPath: Array<string>, value: any): ImmutableObject<any>;
interface AsMutableOptions {
deep: boolean;
}
asMutable(): T;
asMutable(opts: AsMutableOptions): T;
interface ImmutableObjectMixin<T> {
set<K extends keyof T>(property: K, value: T[K]): ImmutableObject<T>;
set<TValue>(property: string, value: TValue): ImmutableObject<T>;
merge(part: any, config?: MergeConfig): ImmutableObject<T>;
setIn<K extends keyof T>(propertyPath: [ K ], value: T[K]): ImmutableObject<T>;
setIn<K extends keyof T, L extends keyof T[K]>(propertyPath: [ K, L ], value: T[K][L]): ImmutableObject<T>;
setIn<K extends keyof T, L extends keyof T[K], M extends keyof T[K][L]>(propertyPath: [ K, L, M ], value: T[K][L][M]): ImmutableObject<T>;
setIn<K extends keyof T, L extends keyof T[K], M extends keyof T[K][L], N extends keyof T[K][L][M]>(
propertyPath: [ K, L, M, N ], value: T[K][L][M][N]): ImmutableObject<T>;
setIn<K extends keyof T, L extends keyof T[K], M extends keyof T[K][L], N extends keyof T[K][L][M], O extends keyof T[K][L][M][N]>(
propertyPath: [ K, L, M, N, O ], value: T[K][L][M][N][O]): ImmutableObject<T>;
setIn<TValue>(propertyPath: string[], value: TValue): ImmutableObject<T>;
update(property: string, updaterFunction: (value: any, ...additionalParamters: any[]) => any, ...additionalArguments: any[]): ImmutableObject<T>;
updateIn(propertyPath: Array<string>, updaterFunction: (value: any, ...additionalParamters: any[]) => any, ...additionalArguments: any[]): ImmutableObject<T>;
asMutable(opts?: AsMutableOptions): T;
without(property: string): ImmutableObject<any>;
without(...properties: string[]): ImmutableObject<any>;
without(filter: (value: any, key: string) => boolean): ImmutableObject<any>;
}
merge(part: DeepPartial<T>, config?: MergeConfig): ImmutableObject<T>;
export interface ImmutableArray<T> {
asMutable(): Array<T>;
asMutable(opts: AsMutableOptions): Array<T>;
asObject(toKeyValue: (item: T) => Array<any>): ImmutableObject<T>;
flatMap(mapFunction: (item: T) => Array<any>): ImmutableArray<any>;
}
update<K extends keyof T>(property: K, updaterFunction: (value: T[K], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject<T>;
update<TValue>(property: string, updaterFunction: (value: TValue, ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject<T>;
// an immutable object is both of Type T (i.e., looks like a normal T) and of type Immutable<T>
export type Immutable<T> = T & (ImmutableObject<T> | ImmutableArray<T>);
updateIn<K extends keyof T>(
propertyPath: [ K ], updaterFunction: (value: T[K], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject<T>;
updateIn<K extends keyof T, L extends keyof T[K]>(
propertyPath: [ K, L ], updaterFunction: (value: T[K][L], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject<T>;
updateIn<K extends keyof T, L extends keyof T[K], M extends keyof T[K][L]>(
propertyPath: [ K, L, M ], updaterFunction: (value: T[K][L][M], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject<T>;
updateIn<K extends keyof T, L extends keyof T[K], M extends keyof T[K][L], N extends keyof T[K][L][M]>(
propertyPath: [ K, L, M, N ], updaterFunction: (value: T[K][L][M][N], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject<T>;
updateIn<K extends keyof T, L extends keyof T[K], M extends keyof T[K][L], N extends keyof T[K][L][M], O extends keyof T[K][L][M][N]>(
propertyPath: [ K, L, M, N, O ], updaterFunction: (value: T[K][L][M][N][O], ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject<T>;
updateIn<TValue>(propertyPath: string[], updaterFunction: (value: TValue, ...additionalParameters: any[]) => any, ...additionalArguments: any[]): ImmutableObject<T>;
export function from<T>(obj: Array<T>, options?: Options): Array<T> & ImmutableArray<T>;
export function from<T>(obj: T, options?: Options): T & ImmutableObject<T>;
without<K extends keyof T>(property: K): ImmutableObject<T>;
without<K extends keyof T>(...properties: K[]): ImmutableObject<T>;
without<K extends keyof T>(filter: (value: T[K], key: K) => boolean): ImmutableObject<T>;
}
export function isImmutable(target: any): boolean;
export function ImmutableError(message: string): Error;
}
interface ImmutableArrayMixin<T> {
asMutable(opts?: AsMutableOptions): T[];
asObject(toKeyValue: (item: T) => [string, any]): ImmutableObject<any>;
flatMap<TTarget>(mapFunction: (item: T) => TTarget[]): ImmutableArray<TTarget>;
}
type ImmutableObject<T> = T & ImmutableObjectMixin<T>;
type ImmutableArray<T> = T[] & ImmutableArrayMixin<T>;
type Immutable<T> = ImmutableObject<T> | ImmutableArray<T>;
function from<T>(obj: T[], options?: Options): ImmutableArray<T>;
function from<T>(obj: T, options?: Options): ImmutableObject<T>;
function isImmutable(target: any): boolean;
function ImmutableError(message: string): Error;
}
declare function SeamlessImmutable<T>(obj: T[], options?: SeamlessImmutable.Options): SeamlessImmutable.ImmutableArray<T>;
declare function SeamlessImmutable<T>(obj: T, options?: SeamlessImmutable.Options): SeamlessImmutable.ImmutableObject<T>;

View File

@ -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<number> = SI.from([0, 1, 2]);
const mutableArray: Array<number> = siArray.asMutable();
const mutableArray2: Array<number> = siArray.asMutable({ deep: true });
const arrayToObject: SI.ImmutableObject<number> = siArray.asObject((value: number) =>
[value.toString(), value]
);
const flatMappedArray: SI.ImmutableArray<number> = 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<User> = SI.from({
firstName: "Mike",
lastName: "test"
});
const extUser: SI.ImmutableObject<ExtendedUser> = siObject.set("address", {
line1: "test"
});
//
// Constructors
// ---------------------------------------------------------------
const updatedAddress: SI.ImmutableObject<ExtendedUser> = extUser.setIn(["address", "line1"], "test2");
const mutableUser: ExtendedUser = updatedAddress.asMutable();
const mutableUser2: ExtendedUser = updatedAddress.asMutable({ deep: true });
const mergedUser: SI.ImmutableObject<User> = siObject.merge({ lastName: "hello" });
const updatedUser: SI.ImmutableObject<ExtendedUser> = extUser.update("firstName", (firstName): string =>
firstName + "hehe"
);
const updatedInUser: SI.ImmutableObject<ExtendedUser> = extUser.updateIn(["address", "line1"], (line): string =>
line + "new address"
);
const userWithoutAddress: SI.ImmutableObject<User> = extUser.without("address");
{
interface User {
firstName: string;
lastName: string;
}
const arrayOfNumbers1: Immutable.ImmutableArray<number> = Immutable.from([0, 2]);
const arrayOfNumbers2: Immutable.ImmutableArray<number> = Immutable([0, 2]);
const user1: Immutable.ImmutableObject<User> = Immutable.from({
firstName: 'Angry',
lastName: 'Monkey'
});
const user2: Immutable.ImmutableObject<User> = 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<User> = Immutable.from<User>([ { firstName: 'Angry', lastName: 'Monkey' } ]);
// asMutable
const mutableArray1: User[] = array.asMutable();
const mutableArray2: User[] = array.asMutable({ deep: true });
// flatMap
const flatMappedArray: Immutable.ImmutableArray<User> = array.flatMap((value: User) =>
[value, value]
);
// asObject
const arrayToObject1: Immutable.ImmutableObject<any> = array.asObject((value) => [value.toString(), value]);
}
//
// Instance syntax: immutable object
// ---------------------------------------------------------------
{
const immutableUser: Immutable.ImmutableObject<User> = Immutable.from({
firstName: 'Pure',
lastName: 'Gold'
});
const immutableUserEx: Immutable.ImmutableObject<ExtendedUser> = 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<User> = immutableUser.set('firstName', 'Whirlwind');
const updatedUser02: Immutable.ImmutableObject<User> = 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<ExtendedUser> = immutableUserEx.setIn(['address', 'line1'], 'Small house');
const updatedUser12: Immutable.ImmutableObject<ExtendedUser> = 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<User> = immutableUserEx.merge({ address: { line1: 'Small house' }, firstName: 'Jack' });
// update: property name is strongly checked
const updatedUser41: Immutable.ImmutableObject<User> = 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<User> = immutableUser.update<string>(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<User> = 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<User> = immutableUserEx.updateIn<string>([ data.propertyId, 'line1' ], x => x.toLowerCase() + ' 43');
// without
const simpleUser1: Immutable.ImmutableObject<User> = immutableUserEx.without('address');
}

View File

@ -19,4 +19,4 @@
"index.d.ts",
"seamless-immutable-tests.ts"
]
}
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }