typescript - 覆盖 Typescript 中一组属性的 getter
问题描述
我想做类似以下的事情:
class Properties {
a = 12;
b = false;
c = 'green';
}
@Injectable()
class PropertyService {
private _properties: Properties;
constructor(props: Properties) {
this._properties = props;
}
// À la Python...
__get__(propname): any {
if (Object.prototype.hasOwnProperty(this._properties, propname)) {
return this._properties[propname];
}
return undefined;
}
}
目标是能够将其用作库中的服务,以便应用程序可以执行以下操作:
import { appProperties } from './config/properties';
...
constructor(private props: PropertyService) {
props.set(appProperties);
}
而且,在图书馆中,我可以执行以下操作:
constructor(private props: PropertyService) { }
someMethod() {
if (this.props.b) {
...
}
}
据我所知,getter 在 Typescript 中必须是特定于属性的。我在这里吠错树了吗?
解决方案
推荐阅读
- html - CSS 过渡不适用于输入宽度
- azure - 尝试通过 Active Directory 访问 Azure Databricks API 时出现错误 403 用户未授权
- java - 为什么错误的泛型转换不会在java中产生异常?
- c# - 如何使用具有某些 Id 的一列加入或搜索 Id
- android - 尝试更新到 Kotlin 1.4.0 时出现奇怪的错误。如何使其与 Gradle 和 IntelliJ IDEA 2020.2.1 一起使用?
- ios - NSUbiquitousKeyValueStore 大部分时间不保存数据
- c++ - 将 std::span 分配给 std::vector 的最简单方法
- r - 使用列名在小标题中更改值
- javascript - CSS 中的 SVG 动画
- python - Python pandas 中的灵活链接