
Static TypeScript: A Static Compiler for the TypeScript Language - syrusakbary
https://www.microsoft.com/en-us/research/publication/static-typescript/
======
ridiculous_fish
This language is quite striking! TypeScript is a superset of JS but here we're
going from the other direction. Make a language that is _only_ the TypeScript
extensions to JS, and drop all the JS parts.

No Object (!!), no Date, TypedArray, RegExp, almost all the mainstays are
gone. It's Java 1.0: static offsets and vtables.

This feels like the beginning of TypeScript throwing its weight around. It has
the momentum to supplant JS and this is an exploratory compatibility-breaking
step.

~~~
siempreb
> Make a language that is only the TypeScript extensions to JS, and drop all
> the JS parts.

So? Is there a reason left then to use Typescript instead of C or C++? I
cannot think of one. Especially for education you better use a real language
that will stay for more than 10 years at least. Microsoft is doing a great job
taking over the Javascript community, and now this.., would love to see their
hidden agenda.

IMAO Typescript is only interesting as a (painful) intermediate solution for
large Javascript code bases until there is a serious replacement for
Javascript through WebAssembly. I'd never choose Typescript as a language,
even stronger, I recently moved away from full-stack web development partly
because of it. Back to C++, what a relief..

~~~
m00dy
C++ is the biggest cancer in the industry. I can't really understand how
people can easily say its name. A C++ programmer needs to understand not only
language syntax but also underlying operating system and its internals. This
is completely different job and therefore needs completely different
expertise. I don't even want to mention hardware limitations, cpu
architectures, etc. So, don't use C++. It is not mainstream anymore and
hopefully it will be a thing of past.

~~~
otabdeveloper4
> A ... programmer needs to understand ... underlying operating system and its
> internals

Oh the humanity!

I weep for mankind and for the industry if this is where we have fallen.

Quick question: are you a Javascript programmer?

~~~
Allower
Well I mean that is the point of abstraction.. if we always had to have our
heads at the lowest level we would not be productive what-so-ever.

~~~
zbentley
"understanding the basics of the operating system internals and how your code
interacts with them" is far, _far_ from the lowest level.

It is also an essential understanding for writing quality, performant code in
any context--web development included, even frontend.

------
pornel
From the title it would seem it is boiled down to a completely statically-
typed language, but it isn't.

e.g. type casts are no-ops. Instead, every object access is type-checked at
runtime. `any` still exists and for unions and complex types it still performs
full lookup by name (instead of RTTI/tagged unions or such).

So it's not really a static language, but a cut-down JavaScript runtime that
instead of optimizing objects with hidden classes at runtime, does similar
thing with vtables ahead of time.

~~~
mmoskal
Exactly. 'Static' refers to control flow which is static (no eval etc.).
Handling of data is dynamic, with static types used as hints.

It's also meant to run on a _very_ constrained microcontrollers (think a few
kB of RAM), and compile inside of a client-side webapp (both of which make it
hard to use things like AssemblyScript).

------
Can_K
See it in action:

[https://arcade.makecode.com/85409-23773-98992-33605](https://arcade.makecode.com/85409-23773-98992-33605)

------
syrusakbary
I believe this will have major implications for the WebAssembly ecosystem.

It also makes me wonder how AssemblyScript will fit into the picture. Anyone
from Microsoft/TypeScript can shed some light on future plans?

~~~
jedisct1
In its current state, Static TypeScript is a toy compared to AssemblyScript.

It doesn’t support much of the original TypeScript features, while
AssemblyScript is getting pretty close to the original TypeScript.

AssemblyScript was also specifically designed to target WebAssembly. For
example, it supports 64 bit types and has builtins that directly map to
WebAssembly opcodes.

Static TypeScript would have to be repurposed as explicitly targeting
WebAssembly in order to become a viable alternative.

And Static TypeScript is part of a set of projects with very different goals.

That being said, it could indeed be turned into something targeting
WebAssembly. And unless Microsoft goes that route, it may happen as a fork.
Still, I’m not sure of the benefits compared to AssemblyScript. I’d rather see
Microsoft contribute to AssemblyScript.

~~~
syrusakbary
Same. I’d like to analyze more time into the project, it seems is mainly for
education proposes?

Either way, I hope Microsoft pushes AssemblyScript forward :)

------
buu700
Given that they're both TypeScript subsets with similar goals, how close is
STS to AssemblyScript? And is WebAssembly also a planned or possible
compilation target of STS?

~~~
darzu
There are no goals to target WebAssembly right now. STS is currently targeted
towards MCUs, which makes it more comparable to uPython or AVR than
AssemblyScript.

------
cztomsik
Here's another one (more mature, I'd say)
[https://github.com/AssemblyScript/assemblyscript](https://github.com/AssemblyScript/assemblyscript)

------
_bxg1
Extremely interesting idea. Sounds like many of the things they get rid of are
some of the more problematic features of JS anyway. The end result is fairly
Swift-y; a loosely OOP static language that has closures and really convenient
hash-map syntax.

------
darzu
Hey HN, I work at MSFT on MakeCode.com [5] which is the primary user of Static
TypeScript. Happy to answer questions. Michal and others who worked on STS
more directly will probably be online in a couple hours.

One important clarification: So far STS has been built specifically to target
microcontrollers/MCUs. Initially it was targeted at the BBC micro:bit [0] for
educational purposes and has since grown to support many more targets
([1],[2],[3],[4],[5]). The other dominant languages in this space are
MicroPython and the AVR/Arduino language. The advantages of STS are beginner
friendliness (makecode is a CS EDU platform), low-memory, small size and
performance (relative to MicroPython). It's worth noting that in general STS
is NOT faster than running JS in V8.

How does it compare to MicroPython?

\- Error reporting: Because STS is statically typed and compiled, most program
errors are caught in the editor before the program is loaded on the device.
Furthermore, STS comes with a simulator that runs in the browser and so
programs can be tested there before they run on hardware. For educational
purposes, we found diagnosing issues on hardware to be really hard for
students, so we try to catch and present errors as early as possible.

\- Performance: [3] is the best example of this. Usually we notice STS is ~10x
faster than uPython while having a smaller flash and memory footprint. This
makes running a game loop at 30FPS feasible on a 120x160 display (e.g. this
[6] $25 device).

How does it compare to AVR/Arduino/C/C++?

\- Modern language: STS, like TypeScript, heavily uses type inference,
supports advanced types constructs like algebraic types (i.e. sum/union and
product/tuples), supports ducktyping, has easy to use lambdas, has a light-
weight syntax, and favors event-based programming.

\- Teachability: Because of the heavy use of type inference, most beginner
programs have no type annotations and it looks just like Javascript. So we
essentially teach JS to users after they want to move on from blocks.

\- In general STS is not faster or smaller than AVR code!

Happy to answer other questions!

[0] [https://makecode.microbit.org](https://makecode.microbit.org)

[1] [https://makecode.adafruit.com](https://makecode.adafruit.com)

[2] [https://maker.makecode.com](https://maker.makecode.com)

[3] [https://arcade.makecode.com](https://arcade.makecode.com)

[4] [https://makecode.mindstorms.com](https://makecode.mindstorms.com)

[5] [https://makecode.com](https://makecode.com)

[6]
[https://www.adafruit.com/product/3939](https://www.adafruit.com/product/3939)

------
craftoman
Yes! I was waiting for this so long, Typescript is the most flexible typed OOP
language and it's way more than just a JS successor.

~~~
hutzlibu
Only that this subset has no objects and is therefore not a OOP language
anymore (?)

~~~
tantalor
It does have objects:

 _STS has at runtime four kinds of unrelated object types:_

 _1\. a dynamic map type has named (string-indexed) properties that can hold
values of any type; 2\. a function (closure) type; 3\. a class type describes
instances of a class, which are treated nominally, via an efficient runtime
subtype check on each field /method access, as described below; 4\. an array
(collection) type._

~~~
hutzlibu
Ok, I did not read the specifications yet and just refered to the other
comment. But your quote gives no indication of (prototyp or classical)
inheritance?

~~~
tantalor
_STS classes are compiled similarly to Java or C# with single inheritance and
static memory layout._

Don't know much about STS or TS but I assume the usual JS class syntax works
as spelled out here:
[https://www.typescriptlang.org/docs/handbook/classes.html](https://www.typescriptlang.org/docs/handbook/classes.html)

------
baybal2
> As a result, the use of scripting languages (such as JavaScript and Python)
> for microcontrollers is on the rise.

I can not think of what they smoke claiming running a scripting language on an
MCU.

Microsoft seem to set its sights on breaking the MCU development after nothing
has left to break on the desktop.

~~~
Dork1234
Python was first created in 1991 where a top of a line computer ran at 50mhz
and had 50 MIPS. A current generation MCU - ATSAMG51 easily runs at 120mhz,
and devices are quickly moving up to the 500mhz range
([https://groboards.com/giant-board/](https://groboards.com/giant-board/)).
There clearly is enough processor power to Python. StaticTypescript isn't even
running scripted, it is compiled to native and has performance close to C/C++
developed code.

------
pininja
Are there any plans to release this?

~~~
abchatra
Already part of Microsoft MakeCode learn to code ecosystem. Read this blog for
more details: [https://www.microsoft.com/en-us/research/blog/rocket-fast-
em...](https://www.microsoft.com/en-us/research/blog/rocket-fast-embedded-
typescript-for-makecode-arcade/?ocid=msr_blog_makearcd_tw)

