📦 ctcpip / proposal-static-types

static types for JavaScript

1 stars 0 forks 👁 1 watching ⚖️ MIT License
📥 Clone https://github.com/ctcpip/proposal-static-types.git
HTTPS git clone https://github.com/ctcpip/proposal-static-types.git
SSH git clone git@github.com:ctcpip/proposal-static-types.git
CLI gh repo clone ctcpip/proposal-static-types
ctcpip ctcpip ✨ behold! types ab69429 2 years ago 📝 History
📁 .github
📄 .gitignore
📄 .npmrc
📄 index.html
📄 LICENSE
📄 README.md
📄 spec.emu
📄 README.md

Static Types

static types for JavaScript

Status

Stage: 0

Champions:

Motivation

dynamic typing is great for some use cases:

let stringOrNumber = 42;
// => 42
typeof stringOrNumber;
// => 'number'

stringOrNumber = "forty-two";
// => "forty-two"
typeof stringOrNumber;
// => 'string'

stringOrNumber += 3;
// => "forty-five"
typeof stringOrNumber;
// => 'string'

stringOrNumber = Number(stringOrNumber)
// => 45
typeof stringOrNumber;
// => 'number'

stringOrNumber -= 3;
// => 42

but sometimes we want to limit our variables to one data type only, which we can't do today. to accomplish this, this proposal adds static type keywords to JavaScript:

data typepossible valuesdefault value
byteintegral numbers -128 to 1271
intintegral numbers -2,147,483,648 to 2,147,483,6471
bootrue or false, but never bothtrue
Objectsame as object{ true: true }
Stringzero or more characters" "
¿String?null, or one or more characters, but can't be empty string" "
notes:

  • the default values of all these types are truthy, because we need more positivity in this world.
  • use of the ¿String? type is generally discouraged due to negativity (and greater risk for falsiness) but is provided as a convenience for the haters.
  • fractional numeric and other types are beyond the scope of this proposal.
  • values can never be null or undefined except ¿String? which can be null if you want to be a hater.
let's see this in action:

byte and int

// instantiate
byte b;
// => 1
typeof b;
// => 'byte'

// compare
let num = 1;
b == num;
// => true
b === num;
// => false

// assign
b = 'byte me';
// => Uncaught TypeError: can't reassign type `byte` to type `String`
b = 333;
// => Uncaught RangeError: don't byte off more than you can chew; 333 is out of range for type `byte`

// implicit casting is permitted:
int i = 333;
// => 333
i = b;
// => 1
typeof i;
// => 'int'

String and ¿String?

String s;
// => " "
typeof s;
// => 'String'

let yee = 'yee';
if(s) { console.info(yee); }
// => yee

// implicit casting is permitted:
s = yee;
s == yee;
// => true
s === yee;
// => false

¿String? letTheHateFlow;
// => null
typeof letTheHateFlow;
// => '¿String?'
letTheHateFlow = "";
// => Uncaught RangeError: absolutely not. stop that.

Object and boo

Object o;
// => { true: true }
typeof o;
// => 'Object'
o.true = false;
// => { true: false }
typeof o.true;
// => 'boo'

boo b;
// => true
typeof b;
// => 'boo'

let c = true;
typeof c;
// => 'boolean'

b == c;
// => true
b === c;
// => false

o.b = b;
o.c = c;

o;
// => { true: false, b: true, c: false }
typeof o.true;
// => 'boo'
typeof o.b;
// => 'boo'
typeof o.c;
// => 'boolean'