🦾 Weekly Topic: Most Challenging Coding Task or Concept

@WoofWoof it is not possible to get a list of the functions/variables exported from a namespace at runtime in MakeCode.

The JavaScript language that MakeCode uses is something we like to call “Static TypeScript”. It’s based off of the TypeScript programming language but has a few restrictions/differences to make it possible for us to compile down to something that can run on extremely low-end hardware (like a micro:bit). You can find out more info on the differences between Static TypeScript and vanilla TypeScript here.

Normal TypeScript gets converted into JavaScript, which is interpreted in your browser or in a runtime like Node.js. I won’t get into how code interpretation works here, but suffice to say it requires hardware with more RAM and CPU power than devices like the micro:bit have. To get around this, we actually compile Static TypeScript from text to binary code much like how a compiler for a language like C++ would. This lets us create code that is much more efficient and can run on inexpensive hardware.

Anyways, back to your original question: in regular TypeScript, you can iterate over the exported functions/variables in a namespace like this. That’s because regular TypeScript implements namespaces as JavaScript objects, which work with Object.keys().

That same code is an error in MakeCode, because we implement namespaces differently.

9 Likes