简体   繁体   中英

Properly type function object argument TypeScript

I have a function somewhere in the code setTouched({...touched, [name]: true }); which accepts an object. The name property is dynamic, it can be 'email', 'password' or whatever. 'touched' object can empty. This is my interface:

interface IInputFieldProps {
    setTouched: () => void;
}

Currently it is just a function that returns nothing. I tried setTouched: ({[key: string]: boolean}) => void . Looks like it doesn't work. How can I properly type object as a function argument using TypeScript?

({[key: string]: boolean}) does not contain types but rather an invalid destructuring. Types must go on the right side of the colon:

interface IInputFieldProps {
    setTouched: (data: {[key: string]: boolean}) => void;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM