Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[experiment] Type definitions for immutability-helper
declare module "immutability-helper" {
type UpdateSpec<T> = {
[P in keyof T]?: UpdateSpec<T[P]> | UpdateSpecCommand<T[P]>
}
interface UpdateSpecCommand<S> {
$set?: S
// FIX ME. $pushm, $unshift, $spliceはSが配列の場合のみ使用できるようにしたい
$push?: S
$unshift?: S
$splice?: SpliceSpecTuple<any>[] // FIX ME. anyは暫定。SはIの配列とし、Iを渡したい
}
// FIX ME. 理想の型定義は[number, number?, ...I]。TypeScript(少なくともv2.4)が対応していない。
// https://stackoverflow.com/questions/44934541/how-to-use-the-spread-operator-in-a-typescript-tuple
interface SpliceSpecTuple<I> {
0: number // startIndex
1?: number // deleteCount
[item: number]: I | number | undefined // insert into the array<I> in place of the deleted elements
}
export default function update<S>(value: S, spec: UpdateSpec<S>): S
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.