是否可以将函数参数类型检查为interface的键之一:
export interface IUser {
id: string;
email: string;
password: string;
}
const updateUserProperty = (property: 'id' | 'email' | 'password') => e =>
this.setState({ [property]: e.target.value });
Run Code Online (Sandbox Code Playgroud)
我不想'id' | 'email' | 'password'被硬编码。
以 JS 的方式,例如。IUser作为一个对象,我可以将其翻译为Object.keys(IUser).join(' | ')