sua*_*kim 2 interface decorator typescript
是否可以使用装饰器通过一些自定义信息来标记接口的某些属性?
最好用一个例子解释一下:
应用状态界面:
export interface AppState {
@persist userData: UserData,
@persist selectedCompany: UserCompany,
// userCompanies should not be persisted since they are always
// fetched afresh from the server...
userCompanies: UserCompany[]
}
Run Code Online (Sandbox Code Playgroud)
保留所有相关状态信息的方法:
persistState(state) {
let newState = {};
Object.keys(state).forEach((key) => {
if (state[key] is decorated with @persist) {
newState[key] = state[key];
}
});
// Persist newState...
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?
如果是这样,我将非常感谢您提供一些资源来指出正确的方向!
如果没有,有没有其他优雅的选择?
是否可以使用装饰器通过一些自定义信息来标记接口的某些属性
不能。接口不能与装饰器一起使用,因为装饰器可以在运行时实际存在的东西之上工作。(接口被删除)。
归档时间: |
|
查看次数: |
2680 次 |
最近记录: |