gpt4 book ai didi

javascript - 扩展运算符破坏了函数定义中的默认属性分配

转载 作者:行者123 更新时间:2023-11-28 17:06:28 24 4
gpt4 key购买 nike

参见https://flow.org/try/#0PTAEAEDMBsHsHcBQAXAngBwKagAoCdZ0BnUAXlAG9FRQBjaAQyKIDkGBbTAfgC5QjkeAJYA7AOYAaanQAWmWgGtMAE16gARrFjRMDEVJoBtAcPEBdPidGTEAXwDciRLVgiBoAMJzFmgB5lQAAoqGnomVg5scgByX2iDWXklZQDIBmgiTASAOlz0AmIpWz58QiIASjIAPkppFzdtTGy4MUDovEwRZUw8RJ9YOPLHW0QgA

// @flow
type Props = {
className?: string,
checked?: boolean,
[string]: string,
};

const Checkbox = ({
className = 'x',
checked = false,
...props,
}: Props) => {
console.log('render checkbox');
}

产生错误:

9:   className = 'x',
^ Unexpected token =
10: checked = false,
^ Unexpected token =
12: }: Props) => {
^ Unexpected token =>

这是有效的 JavaScript,但被流程拒绝。删除 ...props 可以消除错误,但我需要此功能。我做错了什么还是这是流程中的错误?

最佳答案

这只是一个 type-o。多余的逗号

// @flow
type Props = {
className?: string,
checked?: boolean,
[string]: string,
};

const Checkbox = ({
className = 'x',
checked = false,
...props
}: Props) => {
console.log('render checkbox');
}

https://flow.org/try/#0PTAEAEDMBsHsHcBQAXAngBwKagAoCdZ0BnUAXlAG9FRQBjaAQyKIDkGBbTAfgC5QjkeAJYA7AOYAaanQAWmWgGtMAE16gARrFjRMDEVJoBtAcPEBdPidGTEAXwDciRLVgiBoAMJzFmgB5lQAAoqGnomVg5scgByX2iDWXklZQDIBmgiTASAOlz0AmI7PnxCIgBKMgA+SmkXN21MbLgxQOi8TBFlTDxEn1g4ssdbRCA

此外,您应该在类型的顶部定义索引器,如下所示:

// @flow
type Props = {
[string]: string,
className?: string,
checked?: boolean
};

关于javascript - 扩展运算符破坏了函数定义中的默认属性分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55736371/

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