M
M
MOV_UA2021-04-20 14:37:17
typescript
MOV_UA, 2021-04-20 14:37:17

How to properly declare a function?

type strORnum = string | number;
interface IUser {
  name: string;
  age: number;
}
type partialState = { [propName in keyof IUser]: strORnum };
interface IContext extends IUser {
  setState?: (value: partialState) => void;
}

const changeHandle = (e: React.ChangeEvent<HTMLInputElement>): void => {
    if (setState !== undefined) {
      const {target: { name: nameProp, value }, } = e;
        setState({ [nameProp]: nameProp === "name" ? value : Number(value) }); <--- Тут ошибка *
    }
  };
Текст ошибки
Argument of type '{ [x: string]: string | number; }' is not assignable to parameter of type 'partialState'.
  Type '{ [x: string]: string | number; }' is missing the following properties from type 'partialState': name, age

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MOV_UA, 2021-04-20
@MOV_UA

Decided as follows

interface IContext extends IUser {
  setState?: (value: Partial<IUser>) => void;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question