我有这样的代码:
import { ApolloClient } from '@apollo/client';
type F = typeof FeaturesManager.features.warrants
export class FeaturesManager {
static features = {
warrants: Symbol('WARRANTS'),
};
client: ApolloClient<{}>;
constructor({ client }: { client: ApolloClient<{}> }) {
this.client = client;
}
getFeatureAvailability(feature: F) {
}
}
new FeaturesManager(new ApolloClient()).getFeatureAvailability(FeaturesManager.features.warrants)
现在,当我改变时,FeaturesManager.features
我也需要改变F
。如何修复此代码?
更新
要添加所有功能,您可以使用keyof
迭代器提取所有键并使用它们对类型进行索引。
对于您上面的示例,代码应如下所示
type F = typeof FeaturesManager.features[keyof typeof FeaturesManager.features]
或者更清洁的方式可能是
type ValueOf<T> = T[keyof T];
type F = ValueOf<typeof FeaturesManager.features>
我注意到的另一件事是您使用的符号可能不是最好的,因为打字稿可能无法识别差异。将枚举或字符串用于功能可能会更好,您将获得更好的打字稿支持。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句