是否可以使用Typescript将私有成员和方法映射为公共的?
假设我有是否可以使用Typescript将私有成员和方法映射为公共的?,typescript,Typescript,假设我有 class MyClass { private A = 5; public printA() { console.log(`A is ${this.A}`) this.updateA(); } private updateA() { this.A = Math.random(); } } 是否可以将其映射为将所有私有数据转换为公共数据?像 const myClassWithAllPublic
class MyClass {
private A = 5;
public printA() {
console.log(`A is ${this.A}`)
this.updateA();
}
private updateA() {
this.A = Math.random();
}
}
是否可以将其映射为将所有私有数据转换为公共数据?像
const myClassWithAllPublic = <somethingHere>new MyClass();
const myClassWithAllPublic=new MyClass();
我的用例如下所示:
我有一个很大的服务,有很多公共API。我想构造修改类状态的调试函数,这样我就可以说“在值7中放入一个”。为此,我想发送myClassWithAllPublic
,在那里我可以修改它,而不会在我的服务类中产生噪音
这是可能的吗?只需简单地设置
myObject['privateProp']=which
,您就可以轻松做到这一点。是的,但它不是类型安全的。在我们的项目中,禁止通过['something']
进行访问(ts lint)keyof
,并且映射类型不适用于private
。至少检查道具键和值类型怎么样?