[bug 反馈]:typescript 非空断言运算符无效

问题描述

class 中使用 ! 非空断言运算符声明一个属性构造结束依旧会被赋值为 undefined,如示例中的 _aaa 属性

  • 版本:>= 3.7
  • 平台:预览

示例代码

import { _decorator } from "cc";

const { ccclass } = _decorator;

@ccclass('absTest')

export abstract class absTest<T> {

    constructor(value: T) {

        this.Update(value);

    }

    private _bbb: T;

    public get bbb(): T {

        return this._bbb;

    }

    public Update(val: T) {

        this._bbb = val;

        this.OnUpdate();

    }

    protected abstract OnUpdate(): void;

}

@ccclass('test1')

export class test1 extends absTest<string>{

    protected OnUpdate(): void {

        this._aaa = "aaaaa";

    }

   

    private _aaa!: string;

    public log(){

        //这里,应该输出 aaaaa,哈哈

        console.log(this._aaa,this.bbb);

    }

}

new test1("test").log();

参考这个帖子

上面的 bug 是错误的,转为了这个

我试了试和引擎没关系吧……

为什么没关系?同样的 tsconfig 配置不同的结果

https://docs.cocos.com/creator/3.8/manual/zh/scripting/language-support.html#typescript

真正的选项在项目设置里,虽然好像某些情况下即使勾选了也不起作用:

https://docs.cocos.com/creator/3.8/manual/zh/scripting/language-support.html#编译选项

不读取的做法可以理解,因为应该是用 babel 进行编译的,大部分前端编译工具在非必要情况下都不读取 tsconfig 的,Cocos 也没选择实现一个读取 tsconfig 文件的配置的特性。

是真的坑 :sweat_smile: 那我给它关了

这个似乎涉及到了继承时成员的赋值顺序,你断点跟一下就一目了然了