Microsoft MakeCode

Typehinting a list of integers

I have recently discovered that the microbit makecode editor requires to know the type of list before runtime. I have been using typehints to achieve this like so:
enemies: List[game.LedSprite] = []
however when trying to make a list of integers I could not get it to work
I have tried all of these:
purge: List[int] = []
purge: List[Number] = []
purge: List[Integer] = []

yet none of these work!
In order to make this work I need to use a really jank solution:
purge = [0]
Surely there must be a way to do this with typehints, if you know please let me know.

It’s number, not Number. That always bites me in the behind :sweat_smile:

1 Like


What about this:

let test: number[] = [];

You can also do this, but then it starts to look like C++: :thinking:

let test: Array<number> = [];

I’m using python and that doesn’t work.

Okay so strangely in a new project this works:
However when I try and use the exact same code in my project where I need this I get this three errors, “Unused label”, “‘number’ only refers to a type, but is being used as a value here” and “Expression expected.”
And now I think I found the problem, for some reason when you put it in a loop or maybe its just when you indent it these errors appear, but when not indented it works fine, very confusing.

:eyes: You are using Python???

Sorry for all the confusion, what I said is for TypeScript. For Python, it’s:

purge: list[int] = []

And float if you are using decimals.

This doesn’t work either…
and I already tried using List[int] as said in the initial post.

List and list are 2 different things! :wink:
I’ve ran out of ideas - Python is still in beta so I would report a bug issue here.

Yeah I know, list is the builtin python type but List is for typehinting.

Okay thank you!

1 Like