1

Is there a way to cast from a base class into a derived one?

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'
chantey
  • 4,252
  • 1
  • 35
  • 40

1 Answers1

1

Looks like in Assemblyscript this is called upcasting.

Currently the only way to achieve it is by using a as B syntax.

const b2:B = a as B   //correct

Other syntax is currently not supported

const b2:B = a        //incorrect - no implicit cast
const b2:B = <B>a     //incorrect - no angle brackets
chantey
  • 4,252
  • 1
  • 35
  • 40