In TypeScript, every variable is associated with a type annotation, these annotations can be divided into two categories:
-
typedThis kind of annotation contains the type information for the corresponding variables, it can be subdivided into the following categories:
-
fully typedProvide sufficient type information for static compilation, the memory layout can be decided by the compiler. E.g.
number,stringandclassare treated as this category. -
incompletely typedProvide limited information about the type, but these information is not enough for defining the memory layout at compile time. E.g.
interfacedefines least required fields of the type, but the field layout is not defined;uniondefines the possible types of a variable, but we can't know what it will be during compilation.
-
-
anyanyis a pure JavaScript dynamic object, compiler have no information aboutany-objects(any-typed objects)
As shown in the graph above, the fundamental processing principles for different type categories in ts2wasm-compiler following these rules:
fully typed: statically compiled and represented as Wasm value types or WasmGC typesany: represented asexternref, and any operation on theany-objectswill be compiled as invoking pre-defined APIs, which means the dynamic objects are managed by external environment.incompletely typed: each specific type requires individual analysis based on the particular circumstances, e.g.:-
unionis actually a dynamic type because the type can change during runtime, so it's treated asany -
user defined interfaceis widely used in TypeScript, treating it as dynamic will largely influence the performance, so we introducedmetato apply static compilation -
builtin objectsare objects provided by JavaScript runtime, TypeScript defines them as interface for type checking purpose.-
Implementing these built-in objects demands a significant amount of effort, so we treat them as
anyby default, this allows us to immediately use the standard library implementation already available in external environment. (see fallback) -
Simultaneously, we are working on static compilation solutions for selected built-in objects (e.g.
String,Array) to improve performance. The priority of static compilation for these objects is determined based on their actual usage frequency.
-
-
Ts2wasm compiler treats TypeScript as a mixed typed language, there are static types such as Class, Primitives, as well as dynamic types such as any and union, the developers should be aware that different types will have different performance impact, it is always recommended to reduce the usage of dynamic types.
| ts type | category | wasm type | access strategy | performance overhead |
|---|---|---|---|---|
| boolean | fully typed | i32 | static | low |
| number | fully typed | f64 | static | low |
| string | fully typed | struct / stringref | static | low |
| class | fully typed | struct | static | low |
| function | fully typed | func | static | low |
| interface | incompletely typed | struct | static + reflection | medium |
| union | incompletely typed | externref | dynamic | high |
| any | any | externref | dynamic | high |
