More than just a Transpiler
[].asdf()
TypeError: [].asdf is not a function
[] + []
[] + []
""
[] + {}
[] + {}
"[object Object]"
{} + []
{} + []
0
{} + {}
{} + {}
"[object Object][object Object]"
V8
{} + {}
NaN
IE
"hello " + 1
Hello 1
"hello " + (-1)
Hello -1
"hello " - 1
NaN
"hello " - 1
"hello" - 1
function add(a,b){ return a+b }
add([], {})
function add(a,b){ return a+b }
add(foo, bar)
let foo = getFoo();
let bar = getBar();
someModule.add(foo,bar);
let foo = [] + []
let bar = {} + []
let baz = [] + {}
let qux = {} + {}
let tix = "" - 123
node.on('click', function() {
console.log('clicked!');
})
node.on('click', () => {
console.log('clicked!');
})
node.on('click', function() {
console.log('clicked!');
})
Transpiled
var funcs = [];
// create a bunch of functions
for (var i = 0; i < 3; i++) {
funcs.push(function() {
console.log(i);
})
}
// call them
for (var j = 0; j < 3; j++) {
funcs[j]();
}
const funcs = [];
// create a bunch of functions
for (let i = 0; i < 3; i++) { // Note the use of let
funcs.push(function() {
console.log(i);
})
}
// call them
for (let j = 0; j < 3; j++) {
funcs[j]();
}
JavaScript
in
Specification
JavaScript
that run in user's
Browsers
JavaScript
in
Specification
JavaScript
that run in user's
Browsers
vs.
You can never provide good documentation without mentioning the types
function leftPad (str, len, ch) {
function add() {
return arguments[0] + arguments[1];
}
function add(num1, num2) {
return num1 + num2;
}
function add(num1, num2) {
return num1 + num2;
}
function add(num1: number, num2: number) {
return num1 + num2;
}
The type annotation is a theorem
and
the function body is the proof
People's name should be called `displayName`
People have a `userId`
type Person = {
displayName: string,
userId: string,
}
function logName(person: Person) {
console.log(person.displayName)
}
over
Tell me or I will not let you
class ThrowItAll
{
void show() throws Exception
{
throw new Exception("turtles");
}
void show2() throws Exception // Why throws is necessary here ?
{
show();
}
void show3() throws Exception // Why throws is necessary here ?
{
show2();
}
public static void main(String s[]) throws Exception // Why throws is necessary here ?
{
ThrowItAll o1 = new ThrowItAll();
o1.show3();
}
}
Infer as much as possible.
If we can't infer it ... that's okay too
let foo = 123;
// .... later
foo.toPrecision(3); // FINE
let foo: any;
foo = 123;
foo = "Hello world";
// .... later
foo.toPrecision(3); // FINE
interface IFoo {
name: string;
}
interface IBar {
name : string;
}
let foo:IFoo = { name: "hello" };
let bar:IBar = { name: "world" };
foo = bar = foo
interface IFoo {
name: string;
}
interface IBar {
name : string;
}
let foo = { name: "hello" };
let bar = { name: "world" };
foo = bar = foo; // FINE
npm install alm -g
Website : alm.tools