Summer Homework #2 💻 🥬

New stuff we learned this week: 🤔

TypeScript

  • functions that do not explicitly return anything should get the return type annotation of void:
function logHowdy(): void {
  console.log("Howdy!");
}
  • but undefined is a valid type as well, so if you explicitly return undefined you can declare that as well:
function returnsUndefined(): undefined {
  return undefined;
}
  • true and false are also types in TS, and can be used in any place you would use a normal type:
let name: string | false = "jared";
name = false;

function getLastName(name: string): string | true {
  if (name === "Beyonce") {
    return true;
  } else if (name === "Kiah") {
    return "Henderson";
  }
  return "Artinian";
}
  • specific strings are also valid types, which are especially useful in union types:
function returnsGoat(): "goat" {
  return "goat";
}

function orderStatus(orderId: string): "pending" | "paid" {
  if (orderId === "123abc") {
    return "pending";
  }
  return "paid";
}
  • you can create your own custom named types (the convention is to use PascalCase) like this:
type OrderStatus = "pending" | "paid";
  • tuples are arrays of defined width and type, and can by typed like this:
type PersonData = [string, number, boolean];

let jared: PersonData = ["Jared", 41, true];

function getWillow(): PersonData {
  return ["willow", 14, false];
}
  • objects can be declared as custom types by specifying the type of each property:
type Person = {
  name: string;
  hasBeard: boolean;
};

const win: Person = {
  name: "Winfield",
  hasBeard: false,
};

// as with all other types, you don't NEED to name the type
function getPet(): { type: string; name: string } {
  return {
    type: "turtle",
    name: "Stalin",
  };
}
  • Typescript is structurally typed — which means that “if it looks like a duck, walks like a ducks, talks like a duck…it’s a duck. That means, for instance, that this works:
class Duck {
  name: string;
  beakLength: number;

  constructor(name: string, beakLength: number) {
    this.name = name;
    this.beakLength = beakLength;
  }
}

// 😎 LOOK! **classes** are also TYPES
function getDuckName(duck: Duck): string {
  return duck.name;
}

// this works because we're passing it an instance
getDuckName(new Duck("Daffy", 11));

// 😎 WORKS! It's not an instance of the `Duck` class
// but it has the same STRUCTURAL TYPE, so TS is OK 👍
getDuckName({ name: "Ducky", beakLength: 4 });

Javascript ES Modules

  • historically javascript has lacked a good module system for sharing code between multiple files.
  • because of this node adopted and popularized a function-based approach called commonjs which used module.exports and a require() function to share code between files.
  • TC39 (the committee responsible for the evolution of the javascript language) decided to add a new syntax for importing/exporting, called “ES Modules”. The syntax looks like this:
import fs from "fs";
import { createServer } from "http";
import { sayGoat } from "./my-goat-module";
  • instad of using module.exports, with ES Modules you use the new export keyword, like so:
export function sayGoat() {
  console.log("goat");
}

Useful Links:


Homework Plan (next class in 3 weeks)

  • 1 day (per week) review all flashcards
  • 1 day (per week) touch typing practice
  • 1 day Typescript Homework
  • 1 day Bash Variable review Assignment
  • more coming soon…

Homework (week 1)

  • Homework (week 2)


  • Typescript Assignment

    • very carefuly and slowly review the Typescript portion of “New Stuff” above ^^.
    • go to https://gitlab.howtocomputer.link/htc/ts-intro-2, fork it, then clone it into ~/node/summer-2 through ssh, then connect to the dir through vscode.
    • create a new branch
    • follow all of the instructions in the comments.
    • to test your work, from the integrated terminal, type tsnode types.ts — the Typescript will bark at you and spew error messages if you did anything wrong, or forgot anything.
    • once you finished everything and tsnode types.ts shows no errors, submit a MR so I can see your code and leave any comments for corrections.

    Bash Variable Review Assignment

    • do (again) steps 2-12 of the CLI #1 assignment from week 8 — the pdf is here
    • then, do the Extra Credit from CLI #2 of that same week (which also deals with bash variables). Slack us with instructions on how to do your madlib.
    ← All homework