Skip to content

Commit

Permalink
Merge a1a87b9 into e5c1382
Browse files Browse the repository at this point in the history
  • Loading branch information
kolodny committed Mar 30, 2020
2 parents e5c1382 + a1a87b9 commit 8a6288d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 32 deletions.
18 changes: 9 additions & 9 deletions index.d.ts
Expand Up @@ -18,32 +18,32 @@ export declare type Spec<T, C extends CustomCommands<object> = never> = (T exten
$apply: (v: T) => T;
} | ((v: T) => T) | (C extends CustomCommands<infer O> ? O : never);
declare type ArraySpec<T, C extends CustomCommands<object>> = {
$push: T[];
$push: ReadonlyArray<T>;
} | {
$unshift: T[];
$unshift: ReadonlyArray<T>;
} | {
$splice: Array<[number, number?] | [number, number, ...T[]]>;
$splice: ReadonlyArray<[number, number?] | [number, number, ...T[]]>;
} | {
[index: string]: Spec<T, C>;
};
declare type MapSpec<K, V> = {
$add: Array<[K, V]>;
$add: ReadonlyArray<[K, V]>;
} | {
$remove: K[];
$remove: ReadonlyArray<K>;
} | {
[key: string]: {
$set: V;
};
};
declare type SetSpec<T> = {
$add: T[];
$add: ReadonlyArray<T>;
} | {
$remove: T[];
$remove: ReadonlyArray<T>;
};
declare type ObjectSpec<T, C extends CustomCommands<object>> = {
$toggle: Array<keyof T>;
$toggle: ReadonlyArray<keyof T>;
} | {
$unset: Array<keyof T>;
$unset: ReadonlyArray<keyof T>;
} | {
$merge: Partial<T>;
} | {
Expand Down
22 changes: 11 additions & 11 deletions index.ts
Expand Up @@ -23,8 +23,8 @@ const getAllKeys = typeof Object.getOwnPropertySymbols === 'function'
: obj => Object.keys(obj);

function copy<T, U, K, V, X>(
object: T extends U[]
? U[]
object: T extends ReadonlyArray<U>
? ReadonlyArray<U>
: T extends Map<K, V>
? Map<K, V>
: T extends Set<X>
Expand Down Expand Up @@ -336,22 +336,22 @@ export type Spec<T, C extends CustomCommands<object> = never> =
| (C extends CustomCommands<infer O> ? O : never);

type ArraySpec<T, C extends CustomCommands<object>> =
| { $push: T[] }
| { $unshift: T[] }
| { $splice: Array<[number, number?] | [number, number, ...T[]]> }
| { $push: ReadonlyArray<T> }
| { $unshift: ReadonlyArray<T> }
| { $splice: ReadonlyArray<[number, number?] | [number, number, ...T[]]> }
| { [index: string]: Spec<T, C> }; // Note that this does not type check properly if index: number.

type MapSpec<K, V> =
| { $add: Array<[K, V]> }
| { $remove: K[] }
| { $add: ReadonlyArray<[K, V]> }
| { $remove: ReadonlyArray<K> }
| { [key: string]: { $set: V } };

type SetSpec<T> =
| { $add: T[] }
| { $remove: T[] };
| { $add: ReadonlyArray<T> }
| { $remove: ReadonlyArray<T> };

type ObjectSpec<T, C extends CustomCommands<object>> =
| { $toggle: Array<keyof T> }
| { $unset: Array<keyof T> }
| { $toggle: ReadonlyArray<keyof T> }
| { $unset: ReadonlyArray<keyof T> }
| { $merge: Partial<T> }
| { [K in keyof T]?: Spec<T[K], C> };
43 changes: 31 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions test.ts
Expand Up @@ -634,3 +634,25 @@ describe('update', () => {
expect(typeof (update as any).newContext()).toBe('function');
});
});

describe('works with readonly arrays', () => {
interface Thing {
bar: {
foo: ReadonlyArray<{ baz: number }>;
};
}

const a: Thing = {
bar: { foo: [ {baz: 1} ] }
};
const b: Thing = {
bar: { foo: [ {baz: 2} ] }
};
expect(update(a, {
bar: {
foo: { $push: b.bar.foo }
}
})).toEqual({
bar: { foo: [{ baz: 1 }, { baz: 2 }]}
});
});

0 comments on commit 8a6288d

Please sign in to comment.