TypeScript I: Types, Functions & Gernerics

Esther Front-End

esther

Dive into Typescript! 🧐
Took a free course on codecademy 🙇🏻‍♀️


Types

  "Hello, world!"  // string
  42               // number
  true             // boolean
  null
  undefined

Annotations

  let typeString: string;
  let typeNumber: number;
  let typeBoolean: boolean;

Functions

Parameter Type Annotations

  function greet(noun: string) {
    console.log(`Hello, ${noun}!`);
  }

Make it optional:

  function greet(name?: string) {
    console.log(`Hello, ${ name || 'stranger' }!`);
  }

Make it default:

  function exponentiation(power = 1) {
    console.log(4 ** power);
  }

Return Types

Inferring Return Type:

  function factOrFiction() {
    return Math.random() >= .5 ? 'true' : 'false';
  }

  const myAnswer : boolean = factOrFiction();

Explicit Return Type:

  function trueOrFalse(value: boolean): boolean {
    if (value) {
      return true;
    }

    return 'false'; // Typescript Error: Type 'string' is not assignable to type 'boolean'.
  }

Void Return Type:

  function sayHello(): void {
      console.log('Hello!')
  }

Complex Types

Arrays

  // One-Dimensional Array
  let zipcodesNH: string[] = ['03255', '02134', '08002', '03063'];
  // Generic Types
  let zipcodesNH: Array<string> = ['03255', '02134', '08002', '03063'];

  // Multi-dimensional Array
  let zipcodes: string[][] = [zipcodesNH]

  // Empty Array
  let axis: string[] = [];
  let coordinates: number[][] = [];

Tuple Type

What’s a Tuple?
An array that has a fixed size of similar or different element types arranged in a particular sequence.

  let profile: [string, number, boolean, number] = ['Kobe', 39, true, 150000];

Function Rest Parameter Explicit Type

  const sumAllNumbers = (...numberList: number[]): number => {
    let sum = 0;
    for (let i=0; i < numberList.length; i++) {
      sum += numberList[i];
    }
    return sum;
  }

  console.log(sumAllNumbers(100, 70, 30));

Spread Syntax

  function modulo(dividend: number, divisor: number): number {
    return dividend % divisor;
  }

  const numbers: [number, number] = [6, 4];

  // Call modulo() with spread syntax
  console.log(modulo(...numbers));
  // No error, prints 2

Enum Type

What’s enum?
A set of possible values for a variable using TypeScript’s complex type. There’re only two tpyes of enum: numeric enum and string enum.

  // Numeric enum
  enum ClassGrade {
    Freshman = 9,
    Sophomore,
    Junior,
    Senior
  };

  // String enum
  enum ClassName {
    Freshman = 'FRESHMAN',
    Sophomore = 'SOPHOMORE',
    Junior = 'JUNIOR',
    Senior = 'SENIOR'
  }

  const studentClass: ClassName = ClassName.Junior;
  const studentGrade: ClassGrade = ClassGrade.Junior;

  console.log(`I am a ${studentClass} in ${studentGrade}th grade.`);
  // Prints "I am a JUNIOR in 11th grade."

Numeric Enum Type Initializers

A numeric enum type starts counting from 0, and the following options are automatically assigned an increasing number when you create it. But you can choose to give specific numbers to each option if you want.

// This numeric enum type begins with a 1, instead of the default 0
enum Weekdays {
  Monday = 1,
  Tuesday,
  Wednesday,
  Thursday,
  Friday
}

// This numeric enum type has only some explicit values
enum Prizes {
  Pencil = 5,
  Ruler,     // No error: value is 6
  Eraser = 10,
  Pen        // No error: value is 11
};

Object

  let car: {make: string, model: string, year: number};

  car = {make: 'Toyota', model: 'Camry', year: 2020};

Type Alias

Tpye alias can be defined any types in TypeScript, such as:

Function Type Alias

A function type alias can be used to annotate a variable.

  type NumberArrayToNumber = (numberArray: number[]) => number;

  let sumAll: NumberArrayToNumber = function(numbers: number[]) {
    let sum = 0;
    for (let i=0; i < numbers.length; i++) {
      sum += numbers[i];
    }
    return sum;
  }

  let computeAverage: NumberArrayToNumber = function(numbers: number[]) {
    return sumAll(numbers)/numbers.length;
  };

  console.log(computeAverage([5, 10, 15]));   // Prints 10

Generic

Generic Type Alias

  type Collection<G> = {
    name: string,
    quantity: number,
    content: G[]
  };

  let bookCollection: Collection<string> = {
    name: 'Nursery Books',
    quantity: 3,
    content: ['Goodnight Moon', 'Humpty Dumpty', 'Green Eggs & Ham']
  };

  let primeNumberCollection: Collection<number> = {
    name: 'First 5 Prime Numbers',
    quantity: 5,
    content: [2, 3, 5, 7, 11]
  };

Generic Function Type Alias

  function sortArray<T>(array: T[]): T[] {
    return array.sort();
  }

  const stringArray = ['apple', 'banana', 'cherry'];
  const sortedStringArray = sortArray(stringArray);
  // returns ['apple', 'banana', 'cherry']

  const numberArray = [3, 1, 2];
  const sortedNumberArray = sortArray(numberArray);
  // returns [1, 2, 3]