-1

有没有办法从基类转换为派生类?

class A{}
class B extends A{}

const b = new B()
const a:A = b //casts to base class ok
const b2:B = a //Error: Type 'A' is not assignable to type 'B'
4

1 回答 1

0

看起来在 Assemblyscript 这被称为upcasting

目前实现它的唯一方法是使用a as B语法。

const b2:B = a as B   //correct

目前不支持其他语法

const b2:B = a        //incorrect - no implicit cast
const b2:B = <B>a     //incorrect - no angle brackets
于 2022-02-28T22:47:48.303 回答