Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Utility function to convert everything to optional

Tags:

flowtype

Many times I find myself passing in as an arg a certain Shape type but where each key is optional, only at least one is required.

For example:

type Shape = {
    +isFetching: boolean,
    +errorFetching: null | string
}

type ShapeOpt = {
    isFetching?: boolean,
    errorFetching?: boolean
}


function set(data: ShapeOpt) {
    for (const key in data) {
        global[key] = data[key];
    }
}

Is there a utility function to convert from Shape to ShapeOpt?

like image 736
Blagoh Avatar asked Jun 04 '26 06:06

Blagoh


1 Answers

There is a $Shape<Type> helper for generating an object type where each key is optional. But I don't know of a way to say that at least one item is required automatically.

like image 197
Adam Avatar answered Jun 07 '26 23:06

Adam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!