assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

Implementing Interface functions throw duplicate identifier

Open MKHenson opened this issue 5 years ago • 5 comments

Hi there, I believe I found a bug. I've read through the documentation and searched the internet to see if I'm misunderstanding something. However nothing seems to jump out to me as incorrect.

Steps to reproduce

  • Go to AS playground or create a new AS project
  • Create a new ts file and paste the following code
interface IFoo {
  doSomething: () => void;
}

export class Bar implements IFoo {
  doSomething(): void {
    
  }
}

Expected result Build should pass without errors. The TS compiler recognizes the code as valid

Actual result ERROR TS2300: Duplicate identifier 'doSomething'.

MKHenson avatar Jun 09 '21 15:06 MKHenson

This is due to declaring doSomething as a property and then in the class you implement it as a method. Changing the interface to declare a method fixes it.

You can also implement doSomething like get doSomething(): () => void { return () => {} }, but since AS currently lacks closures you wouldn't have access to this.

willemneal avatar Jun 09 '21 15:06 willemneal

Yes, that's a bug. Quick workaround is using doSomething(): void; instead doSomething: () => void;.

@dcodeIO will be great generalize function expressions and function declarations

MaxGraey avatar Jun 09 '21 15:06 MaxGraey

Not easy I fear, because one is a mutable field one can re-assign that is called indirectly while the other is an immutable method called directly. Perhaps with a readonly modifier, not sure.

dcodeIO avatar Jun 09 '21 15:06 dcodeIO

Wait is it possible reassign expression based method in AS?

MaxGraey avatar Jun 09 '21 15:06 MaxGraey

Ah that makes sense! I was getting thrown off by the TS compiler not flagging anything, and yet the AS was flagging it. Thanks guys. Really excited to see where AS goes :)

MKHenson avatar Jun 09 '21 16:06 MKHenson