Is it possible to typecheck a function argument to be one of the interface's keys:
export interface IUser {
id: string;
email: string;
password: string;
}
const updateUserProperty = (property: 'id' | 'email' | 'password') => e =>
this.setState({ [property]: e.target.value });
I'd like 'id' | 'email' | 'password' not be hardcoded.
In a JS way eg. IUser being an object, I can translate that to Object.keys(IUser).join(' | ')
Yes you can:
export interface IUser {
id: string;
email: string;
password: string;
}
const updateUserProperty = (property: keyof IUser) => e =>
this.setState({ [property]: e.target.value });
updateUserProperty("sdsd"); //Error
updateUserProperty("id"); //Ok
More info here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With