gpt4 book ai didi

javascript - 流量: Simpler way to annotate `type | typeof undefined` ?

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

我经常遇到同样的流程问题:我将某些内容注释为 ?type 并忘记它接受 null 所以当我做这样的事情时:

function foo(data: string = '') {}
function bar(data: ?string) { foo(data); }

flow 对我大喊大叫,因为 foo(null) 没有将 data 解析为 string,而是 foo(undefined) 确实如此。那么有没有更简单的写法:

function bar(data: string|typeof undefined) {

或者我做的事情根本就是错误的吗?

最佳答案

typeof undefined 在流程中是 void

那么呢:

function bar(data: string | void | null) {

function bar(data?: string | null) {

关于javascript - 流量: Simpler way to annotate `type | typeof undefined` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48534652/

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