以下打字稿代码按预期工作,但打字稿在 test2 和 test3 属性上引发 TS2564 错误。是因为它们是在方法内部初始化的吗?这样的类应该怎么写。
我相信它会按预期工作,因为它在 javascript 中是有效的。难道是打字稿设置错误造成的?
class class1{
test1:number;//test1 is initialized as expected
test2:number;//Property 'test2' has no initializer and is not definitely assigned in the constructor.
test3:number;//Property 'test3' has no initializer and is not definitely assigned in the constructor.
constructor(){
//test1
this.test1 = 0;
//test2
const setTest2To0 = () =>{
this.test2 = 0;
};
setTest2To0();
//test3
this.setTest3To0();
}
setTest3To0(){
this.test3 = 0;
}}
Run Code Online (Sandbox Code Playgroud)