1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159# Static Types
static types for JavaScript
## Status
Stage: 0
Champions:
- Chris de Almeida ([@ctcpip](https://github.com/ctcpip))
## Motivation
dynamic typing is great for some use cases:
```js
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 type | possible values | default value |
|---|---|---|
| `byte` | integral numbers `-128` to `127` | `1` |
| `int` | integral numbers `-2,147,483,648` to `2,147,483,647` | `1` |
| `boo` | `true` or `false`, but never both | `true` |
| `Object` | same as `object` | `{ true: true }` |
| `String` | zero 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`
```js
// 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?`
```js
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`
```js
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'
```