Primivite types: boolean
, string
, number
, object
, function
Hello, folks! I'm not sure if this has been proposed like this before, so here I come.
What if we have an optional type annotation following the typescript colon
syntax, that allow us to tell the runtime variables are constrained to a single primitive type.
- The notation only works for variable definitions, not for object properties.
- The runtime would only allow assignments to typed variables in case the types are equal, or explicitly casted (eg.:
Number('345')
) - Above also applies to calling a function with typed parameters.
- Same also applies to function return types
- The runtime would probably be able to apply some optimization to those variables.
What this proposal is NOT
- support for creating object schemas with property types, generics, interface and other typescript constructs
- type annotation as comment (GitHub - tc39/proposal-type-annotations: ECMAScript proposal for type syntax that is erased - Stage 1 1)
Sample Code
let a: boolean = true;
let b: number;
b = 2;
let c: string;
c = String(3456);
function foobar(a: string, b): string {
return a.toUpperCase().slice(b);
}
c = foobar(c, b);
Extra points yet to address:
- how null and undefined fits in that model
- callback type information
- whether runtime should check available properties and methods for each type