Skip to content

Instantly share code, notes, and snippets.

@acutmore
Last active December 20, 2020 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acutmore/c3686968c5635a08e86b99bdd1d0d0b7 to your computer and use it in GitHub Desktop.
Save acutmore/c3686968c5635a08e86b99bdd1d0d0b7 to your computer and use it in GitHub Desktop.
TypeScript presentation - use https://www.typescriptlang.org/play and load gist with the slides plugin enabled https://github.com/orta/playground-slides

alt text


  • Erasable
  • Gradual
  • Structural
  • Generic
  • Inferable
  • Expressive
  • Object-oriented
  • Functional

TSConf 2020: Keynote - Anders Hejlsberg https://youtu.be/IGw2MRI0YV8?t=379


Erasable

Apart from a few exceptions TypeScript does not impact the runtime JavaScript

interface StringProp { prop: string; }

function getProp(arg: StringProp): string { return arg.prop; }


(not) Erasable

  • enums
  • namespace
  • parameter properties
  • experimental decorators
// Enums

enum E { a = 1, b = 2, }

enum E { c = 3, }

const enum E2 { a = 10 ** 2 }

let x = E2.a;

// namespace

namespace YouView { export const version = 100; }

console.log(YouView.version);

// parameter properties

class Foo { constructor(public x: string) {} }

// experimental decorators declare var decorate: any;

@decorate() class Bar { @decorate method() {

} }


Gradual

You can start with only JavaScript and gradually add types

  • noEmitOnError: false
  • noImplicitAny: false
function getProp(arg): string { // arg is 'any' return arg.prop; }

Structural

  • As opposed to nominal (by name)
class A { getString() { return 'from a'; } }

function callMethod(arg: A): string { return arg.getString(); }

class B { private s = 'from b';

getString() { return this.s; } }

let b = new B(); callMethod(b);


(Not) Structural

  • Enums
const enum Colour { BLUE, RED }

const enum Day { MONDAY, TUESDAY }

function setColour(c: Colour) {}

setColour(Colour.BLUE); setColour(0); setColour(Day.MONDAY);


Generic

interface GenericProp<T> { prop: T; }

function getProp<T>(arg: GenericProp<T>): T { return arg.prop; }


Inferable

interface GenericProp<T> { prop: T; }

function getProp<T>(arg: GenericProp<T>): T { return arg.prop; }

let x = getProp({ prop: [1, 2, 3] }); // x: number[];


Expressive

In computer science, the expressiveness of a language is the breadth of ideas that can be represented and communicated in that language.

https://en.wikipedia.org/wiki/Expressive_power_(computer_science)

// Adding props to objects function addProp<Obj, Prop extends string, Value>( obj: Obj, prop: Prop, value: Value ): Obj & { [key in Prop]: Value } { let newProp = {[prop]: value} as {[key in Prop]: Value}; return {...obj, ...newProp}; }

let x = addProp({ a: 1, b: '2' }, 'c', [3]); x.a; x.b; x.c;

// Manipulating strings let c = reverseString('abcdefgh');

type ReverseString<S extends string> = S extends '' ? '' : S extends `${infer Char}${infer Remaining}` ? `${ReverseString<Remaining>}${Char}` : '';

function reverseString<S extends string>(s: S) { return s.split('').reverse().join('') as ReverseString<S>; }


Object-oriented

  • Support for common OO patterns (abstract classes, private fields)
  • Understands JS class runtime semantics
abstract class Animal { protected abstract sound: string;

makeSound(): this { console.log(this.sound); return this; } }

class Dog extends Animal { sound = 'woof';

walk() { console.log('walk') } }

Dog.prototype.makeSound;

let pet = new Dog(); pet.makeSound().walk();

let pet2: Animal = new Dog(); pet2.makeSound().walk();


Functional

  • Supports functional programming patterns
type Fun<A, B> = (a: A) => B;

// composing function compose<A, B, C>( f1: Fun<A, B>, f2: Fun<B, C> ): Fun<A, C> { return (a) => f2(f1(a)); }

let makeArray = compose((x: string) => parseInt(x), n => new Array(n));

let result1 = makeArray('10');

// currying let add = (a: number, b: number) => a + b;

let add1 = add.bind(null, 1);

let oneHundred = add1(99);


Fin :)

@acutmore
Copy link
Author

youtube

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment