H
H
Hackerbot14882020-07-15 12:47:22
typescript
Hackerbot1488, 2020-07-15 12:47:22

How to set the type of a function parameter?

[CHANGE_THEME]: (state: stateProps, { payload }): stateProps => ({
    ...state,
    ...payload,
  }),

How to set payload type?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twoone, 2020-07-15
@twoone

It can be like this -

interface StateProps {
    a: number;
    b: string;
    c: boolean;
}

interface Action<P> {
    payload: Partial<P>;
}

const f = <P>(state: P, { payload }: Action<P>): P => ({
    ...state,
    ...payload,
});

let nextState = f(
    { a: 0, b: '', c: true },
    { payload: { c: false } }
);

The only thing to remember is that generic functions are not valid constructs in files with the .tsx.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question