1.9.1版本 JS升级到TS引发的错误:Object prototype may only be an Object or null

1.9.1版本,老项目需要升级成ts,为了能够兼容不报错,每个js添加声明文件,问题继承出现了报错。

Cocos Helper新建一个typescript项目,在helloword.ts中,去继承base.js,使用import方式会导致下图错误。
解决方法:换成require不会报错,但是失去了代码提示,是否还有更好但方式解决?

// helloword.ts
const {ccclass, property} = cc._decorator;
import Base from './base'

@ccclass
export default class Helloworld extends Base {
  ...
}

下面

// base.js
cc.Class({
  extends: cc.Component,
  properties: {},

  init() {
    console.log('init class')
 }
});


// base.d.ts
export default class base extends cc.Component {
   public init: void;
 }

load script [/Users/tsDemo/temp/quick-scripts/assets/Script/Helloworld.js] failed : TypeError: Object prototype may only be an Object or null: undefined

Cocos Creator 版本1.9.1 最小复现代码
tsDemo.zip (277.4 KB)

1赞

试了个勉强能用的写法.

:+1:试了下,这种方式确实能运行,也有了代码提示。 用着有点麻烦,还不好理解。