Blagoh
Blagoh

Reputation: 1235

Utility function to convert everything to optional

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?

Upvotes: 3

Views: 45

Answers (1)

Adam
Adam

Reputation: 2287

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.

Upvotes: 2

Related Questions