TypeScript is an open source programming language created in 2012 by Anders Hejlsberg.
git clone https://github.com/microsoft/TypeScript
#31on PLDB | 11Years Old | 3mRepos |
TypeScript is a free and open-source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript, and adds optional static typing to the language. Anders Hejlsberg, lead architect of C# and creator of Delphi and Turbo Pascal, has worked on the development of TypeScript. Read more on Wikipedia...
console.log("Hello, world!");
console.log("Hello World");
// Hello world in TypeScript
alert('Hello world!');
console.log("Hello, World!");
class Person {
private name: string;
private age: number;
private salary: number;
constructor(name: string, age: number, salary: number) {
this.name = name;
this.age = age;
this.salary = salary;
}
toString(): string {
return `${this.name} (${this.age}) (${this.salary})`; // As of version 1.4
}
}
Feature | Supported | Token | Example |
---|---|---|---|
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 |
|
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 /// |
|
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"; /// |
|
Type Casting | โ |
|
|
Classes | โ | class Person {} |
|
Booleans | โ | const result = true |
|
Generics | โ | function identity |
|
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 | โ | ||
Operators | โ | 1 + 1 |
|
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") |
|
Labels | โ | main: console.log("pldb") |
|
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; |
|
hasExports | โ | export function myFunction() { } |
|
hasSymbols | โ | // A symbol is a unique and immutable primitive value, often used as a unique key for object properties pldb = Symbol() |
|
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 | ||
S-Expressions | X |