Microsoft MakeCode

Type annotation cannot appear on a constructor declaration

Im trying out some stuff for a project and I have found it that I would prefer to use classes for the thing im doing. The problem occurs when having a constructor. It then returns me with a error “Line 3: Type annotation cannot appear on a constructor declaration.”.

Here is the test code:

I also tried to do it according to the microbit python docs. But that returns me with “Line 4: the field ‘string’ of ‘Testclass’ is static; Line 7: the field ‘string’ of ‘Testclass’ is static”

Here is the code for that:

1 Like

I run into the same problem too, any advise here?

This isn’t a solution, but it seems like this is related to Python parsing of constructors. MakeCode translates Python:

class Testclass:
    def __init__(self, text):
        self.string = text

into TypeScript like:

class Testclass {
    string: string
    constructor(text: string): Testclass {
        this.string = text

Note the Testclass annotation on the constructor (an error). It can be removed from the JavaScript, but then the resulting code can’t be translated back to Python.

Even examples given in pxt seem to fail. It may help to raise an issue via