More than just a Transpiler
(google basarat)
#TypeScript #NDCSydney
[].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 = getServerFoo();
let bar = getServerBar();
someModule.add(foo,bar);
let foo = [] + []
let bar = {} + []
let baz = [] + {}
let qux = {} + {}
let tix = "" - 123
Linter
+
Transpiler
=
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]();
}
var funcs = [];
// create a bunch of functions
for (var i = 0; i < 3; i++) {
funcs.push(function() {
console.log(i);
})
}
// 3 , 3 , 3
var funcs = [];
var i = 0;
// create a bunch of functions
for (; i < 3; i++) {
funcs.push(function() {
console.log(i);
})
}
// 3 , 3 , 3
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]();
}
var funcs = [];
// create a bunch of functions
for (let i = 0; i < 3; i++) { // Note the use of let
funcs.push(function() {
console.log(i);
})
}
// 0 , 1 , 2
JavaScript
still in
Specification
JavaScript
that run in user's
Browsers
JavaScript
in
Specification
JavaScript
that run in user's
Browsers
Types
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): number {
return num1 + num2;
}
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
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();
}
}
let foo = 123;
// .... later
foo.toPrecision(3); // FINE
let foo: any;
foo = 123;
foo = "Hello world";
// .... later
foo.toPrecision(3); // FINE
type Model = {
name: string;
}
type ViewModel = {
name : string;
}
let foo: Model = { name: "hello" };
let bar: ViewModel = { name: "world" };
foo = bar = foo; // FINE
way too many Foo -> JS languages
let foo: number = 123;
foo = "hello";
npm install alm -g
Website : alm.tools