{ const history = [...this.state.hist-6ren">
gpt4 book ai didi

javascript - 如何将 "fallback"值添加到流程中的可选函数参数?

转载 作者:行者123 更新时间:2023-12-03 02:24:25 27 4
gpt4 key购买 nike

我有以下使用流类型的函数

  push = (pathname: string, data?: Object) => {
const history = [...this.state.history, pathname];
this.setState({
history,
pathname,
data
});
};

在普通的 javascript 中,我可以执行类似 (pathname, data = null) 的操作,其中如果未提供数据,则 null 将用于数据,但我无法弄清楚语法使用流类型时可实现此目的。

最佳答案

let push = (pathname: string, data?: Object = {a: 1}) => {
// ...
};

此外,您还可以将默认值与解构一起使用:

type Arg = { prop: number };
const func = ({ prop = 1 }: Arg) => prop;

关于javascript - 如何将 "fallback"值添加到流程中的可选函数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49013021/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com