Introduction to TypeScript (Part 3)

Function Typing

Optional Parameters

  • If a function or constructor is given fewer arguments than required, the function or constructor shall behave exactly as if it had been given sufficient additional arguments, each such argument being the undefined value
  • If a function or constructor described in this clause is given more arguments than specified, the extra arguments are evaluated by the call and then ignored by the function
function f(x?: number) { ... }
function f(x = 5) { ... }

Function Overloading

// Function Signatures - Deleted from TypeScript during transpilationfunction makeDate(timestamp: number): Date;function makeDate(m: number, d: number, y: number): Date;// Function implementation - what gets used in JavaScriptfunction makeDate(mOrTimestamp: number, d?: number, y?: number): Date {
// implementation
}

Function Type Expressions

function greeter(fn: (a: string) => void) {
fn("Hello, World!");
}
// or alternativelytype GreeterFunction = (a: string) => void
// Then utilising these:
function greeter(fn: GreetFunction) { ... }

Object Typing

Index Signatures

interface StringArray { [index: number]: string; }// ortype NumberArrayWithStringKeys { [index: string: number ;}// another exampletype OnlyBooksArray { [index: string]: Book; }

Readonly Properties

interface SomeType { readonly prop: string }

Intersection Types

type ColorfulCircle = Colorful & Circle;

Extending Types

interface AddressWithUnit extends BasicAddress {
unit: string;
}

Tuple Types

type StringNumberPair = [string, number]function doSomething(pair: [string, number]){
// Both are equivalent
const [a, b] = pair;
a = pair[0];
b = pair[1];
..do something with a & b
}

Class Typing

Abstract Types

abstract class Base {
abstract getName(): string;

printName() {
console.log("Hello, " + this.getName());
}
}
const b = new Base(); // Error

class Derived extends Base {
getName() {
return "world";
}
}

const d = new Derived(); // Okay

Implementing Interfaces

interface Pingable {
ping(): void;
}

class Sonar implements Pingable {
ping() {
console.log("ping!");
}

Public, Private and Protected Class Members

class Base {
private x = 0; // <-- Only the Base class can access
public printX() { console.log(x); } <-- Anybody can call this protected greeting() { console.log("Hi") } <-- Subclasses can call
}

The “this” keyword in TypeScript

Constructor Shorthand in TypeScript

class Params {
constructor(public readonly x: number, protected y: number,
private z: number)
{
// No body necessary
}
}

Rounding it Out: Structural Comparisons

class Point1 {  x = 0;  y = 0; }

class Point2 { x = 0; y = 0; }

// OK
const p: Point1 = new Point2();
class Person {
name: string;
age: number;
}

class Employee {
name: string;
age: number;
salary: number;
}

// OK
const p: Person = new Employee();

Next Steps

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ashok Khanna

Ashok Khanna

Masters in Quantitative Finance. Writing Computer Science articles and notes on topics that interest me, with a tendency towards writing about Lisp & Swift