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(' | ')
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(' | ')
1 Answer
Reset to default 9Yes 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.