Skip to content

Instantly share code, notes, and snippets.

@masahirompp
Created July 27, 2017 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masahirompp/65d155bd87ba256846cc2dda563067c2 to your computer and use it in GitHub Desktop.
Save masahirompp/65d155bd87ba256846cc2dda563067c2 to your computer and use it in GitHub Desktop.
[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