在 typescript 中使用 immutability-helper 并做出反应

Using immutability-helper in typescript and react

我从 @types/immutability-helper 导入了 update。现在我想在 React 的组件状态下将一个项目推送到数组。

index.d.ts 文件中的语法为:

interface UpdateFunction {
    (value: any[], spec: UpdateArraySpec): any[];
    (value: {}, spec: UpdateSpec): any;
    extend: (commandName: string, handler: CommandHandler) => any;
}

并且 UpdateArraySpec

interface UpdateArraySpec extends UpdateSpecCommand {
    $push?: any[];
    $unshift?: any[];
    $splice?: any[][];
    [customCommand: string]: any;
}

这是否意味着我必须写 2 个更新?:

this.setState(update(this.state, update(this.state.Markers, { $push: [info] })));

或者什么?

这是对我有用的语法:

this.setState(update(this.state, { Markers: { $push: [info] } }));

这在打字稿定义文件中并不明显。但是 the docs 说了类似的话。