dmn*_*sgn 3 javascript properties interface typescript
是否可以将函数参数类型检查为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(' | ')
是的你可以:
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
Run Code Online (Sandbox Code Playgroud)
更多信息在这里。
| 归档时间: |
|
| 查看次数: |
3449 次 |
| 最近记录: |