MultiLine Comments |
β |
/* */ |
/* A comment
*/ |
Comments |
β |
|
// A comment |
Algebraic Data Type |
β |
|
declare type numOrString = string | number |
Line Comments |
β |
// |
// A comment |
Union Types |
β |
|
declare type numOrString = string | number |
Single-Type Arrays |
β |
|
const scores: int[] |
Type Inference |
β |
|
|
Strings |
β |
" |
"hello world" |
Type Parameters |
β |
|
function identity(arg: T): T {
return arg;
} |
Static Typing |
β |
|
|
Inheritance |
β |
|
class B {}
class A extends B {} |
Print() Debugging |
β |
console.log |
console.log("Hi") |
Namespaces |
β |
|
// Typescript even supports splitting namespaces across multiple files:
// Validation.ts
namespace Validation {
export interface StringValidator {
isAcceptable(s: string): boolean;
}
}
// LettersOnlyValidator.ts
///
namespace Validation {
const lettersRegexp = /^[A-Za-z]+$/;
export class LettersOnlyValidator implements StringValidator {
isAcceptable(s: string) {
return lettersRegexp.test(s);
}
}
} |
Mixins |
β |
|
// https://www.typescriptlang.org/docs/handbook/mixins.html
class SmartObject implements Disposable, Activatable {
}
// Note: still need to do some runtime ops to make that work. |
Interfaces |
β |
|
// https://www.typescriptlang.org/docs/handbook/interfaces.html
interface SquareConfig {
color?: string;
width?: number;
} |
File Imports |
β |
|
import { ZipCodeValidator } from "./ZipCodeValidator";
///
///
import moo = module('moo');
/// |
Type Casting |
β |
|
something; |
Classes |
β |
|
class Person {} |
Booleans |
β |
|
const result = true |
Generics |
β |
|
function identity(arg: T): T {
return arg;
} |
Abstract Types |
β |
|
abstract class Animal {}
class Dog extends Animal |
Access Modifiers |
β |
|
class Person {
private _age = 2
public get age() {
return _age
}
protected year = 1990
} |
Static Methods |
β |
|
class Person {
static sayHi() {
console.log("Hello world")
}
} |
Enums |
β |
|
enum Direction {
Up,
Down
} |
Scientific Notation |
β |
|
|
Binary Literals |
β |
|
// 0[bB][01]+n?
0b100110100000110011110010010 |
Floats |
β |
|
// (\.[0-9]+|[0-9]+\.[0-9]*|[0-9]+)([eE][-+]?[0-9]+)?
80766866.0 |
Hexadecimals |
β |
|
// 0[xX][0-9a-fA-F]+n?
0x4D06792 |
Octals |
β |
|
// 0[oO]?[0-7]+n?
0o464063622 |
Sets |
β |
|
set = new Set()
set.add("foo") |
Function Composition |
β |
|
function o(f, g) {
return function(x) {
return f(g(x));
}
} |
Destructuring |
β |
|
const o = {p: 42, q: true};
const {p, q} = o; |
Default Parameters Pattern |
β |
|
function multiply(a, b = 1) {
return a * b;
} |
Increment and decrement operators |
β |
|
let i = 0
i++
i-- |
Methods |
β |
|
class Person {
method1() {}
method2() {}
} |
Functions |
β |
|
function helloWorld() {console.log("hi")} |
Case Sensitivity |
β |
|
|
Zero-based numbering |
β |
|
|
While Loops |
β |
|
let times = 10
while (times) {times--}
console.log("done") |
Ternary operators |
β |
|
let i = true ? 1 : 0 |
Switch Statements |
β |
|
var animal = "dog"
switch (animal) {
case "dog": console.log("yay"); break;
case "cat": console.log("oh"); break;
} |
Letter-first Identifiers |
β |
|
|
References |
β |
|
|
Multiline Strings |
β |
|
const lines = `one
two` |
Anonymous Functions |
β |
|
(() => console.log("hello world"))() |
Infix Notation |
β |
|
const six = 2 + 2 + 2 |
Implicit Type Casting |
β |
|
console.log("hello " + 2) |
Assignment |
β |
|
var name = "John" |
Directives |
β |
|
"use strict";
"use asm"; |
Generators |
β |
|
function* fibonacci(limit) {
let [prev, curr] = [0, 1];
while (!limit || curr <= limit) {
yield curr;
[prev, curr] = [curr, prev + curr];
}
}
// bounded by upper limit 10
for (let n of fibonacci(10)) {
console.log(n);
}
// generator without an upper bound limit
for (let n of fibonacci()) {
console.log(n);
if (n > 10000) break;
}
// manually iterating
let fibGen = fibonacci();
console.log(fibGen.next().value); // 1
console.log(fibGen.next().value); // 1
console.log(fibGen.next().value); // 2
console.log(fibGen.next().value); // 3
console.log(fibGen.next().value); // 5
console.log(fibGen.next().value); // 8
// picks up from where you stopped
for (let n of fibGen) {
console.log(n);
if (n > 10000) break;
} |
Garbage Collection |
β |
|
|
First-Class Functions |
β |
|
[2.0,1.1].map(Math.round) |
Exceptions |
β |
|
try {
undefinedFn()
} catch (err) {
console.log(err)
} |
hasDynamicTyping |
β |
|
|
Constants |
β |
|
const one = 1 |
Constructors |
β |
|
class Person {
constructor(name) {
this._name = name
}
}
new Person("Jane") |
Conditionals |
β |
|
if (true)
console.log("hi!") |
Method Chaining |
β |
|
"hello world".toString().substr(0, 1).length |
Magic Getters and Setters |
β |
|
// Can be implemented in ES6 using proxies:
"use strict";
if (typeof Proxy == "undefined") {
throw new Error("This browser doesn't support Proxy");
}
let original = {
"foo": "bar"
};
let proxy = new Proxy(original, {
get(target, name, receiver) {
let rv = Reflect.get(target, name, receiver);
if (typeof rv === "string") {
rv = rv.toUpperCase();
}
return rv;
}
});
console.log(`original.foo = ${original.foo}`); // "original.foo = bar"
console.log(`proxy.foo = ${proxy.foo}`); // "proxy.foo = BAR" |
Dynamic Properties |
β |
|
class Person {}
const person = new Person()
person.age = 50 |
Source Maps |
β |
|
{
version: 3,
file: 'min.js',
names: ['bar', 'baz', 'n'],
sources: ['one.js', 'two.js'],
sourceRoot: 'http://example.com/www/js/',
mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
}; |
Bitwise Operators |
β |
|
var x = 5 & 1; |
Single Dispatch |
β |
|
|
Polymorphism |
β |
|
"a" + "b"; 1 + 2 |
Merges Whitespace |
β |
|
|
Lists |
β |
|
const list = [1,2,3] |
Integers |
β |
|
80766866 |
Breakpoints |
β |
|
if (false)
debugger |
Partial Application |
β |
|
const addNumbers = (num1, num2) => num1 + num2
const add5 = num => addNumbers(10, num) |
Map Functions |
β |
|
[1,2.1].map(Math.round) |
Binary Operators |
β |
|
1 + 1 |
Async Await |
β |
|
async doSomething => await somethingElse() |
Expressions |
β |
|
1 + 1 |
Regular Expression Syntax Sugar |
β |
|
console.log("Hello World".match(/\w/)) |
Statements |
β |
|
let x = 3; |
Case Insensitive Identifiers |
X |
|
|
Semantic Indentation |
X |
|
|
Operator Overloading |
X |
|
|
Multiple Inheritance |
X |
|
|
Function Overloading |
X |
|
|
Macros |
X |
|
|
Processor Registers |
X |
|
|
Multiple Dispatch |
X |
|
|
Pointers |
X |
|
|
Variable Substitution Syntax |
X |
|
|