DEV Community

Discussion on: Typescript and Redux. My tips.

Collapse
 
pretaporter profile image
Maksim

We need to collect all our return types. Straight approach is union:

ReturnType<typeof actionCreatorOne> | ReturnType<typeof actionCreatorTwo>

Let's automate this. We use extends for checking is object.

const someObj = { field: 'Hello World!' };

type isObject<T> = T extends { [key: string]: unknown } ? T : never;

isObject<typeof value>; // { field: 'Hello World!' }
isObject<'Hello World!'>; // never

And then we should extract object values:

type InferValueTypes<T> = T extends { [key: string]: infer U } ? U : never;

Almost the same as isObject

Collapse
 
andrasnyarai profile image
Andras Nyarai

ahh i c so the ‘T extends { [key: string] infer U }’ will evaluate to true if T does extends the provided object shape.
thnks