Created
July 27, 2017 02:18
-
-
Save masahirompp/65d155bd87ba256846cc2dda563067c2 to your computer and use it in GitHub Desktop.
[experiment] Type definitions for immutability-helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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