Typescript Cheat Sheet

dayvster.com
type
Defines a new type, describing the property names and value types of an object.
interface
Same as type, but can be used to describe function types.

Common Types:

string
Stores long text
number
Stores numbers
boolean
Stores either true or false
any
Stores any type
NOTE: Avoid using any as much as possible
void
Represents the absence of a value
NOTE: Usually used as the return type of a function
Arrays
string[] 
number[] 
boolean[] 
any[] 
ICustomType[]
NOTE: Any type can be used in an array by simply adding [] to the end function
const
Creates a variable that cannot be changed
let
Creates a variable that can be changed
var
Creates a variable that can be changed
NOTE: Avoid using var as much as possible
function
Creates a function that can be called
=>
Arrow function, used to create a function that can be called
Example:
const myFunction = (param1: string, param2: number) => {
  // Do something
}
export
Exports a variable, function, or class
import
Imports a variable, function, or class
const {value} = object
Destructures an object, allowing you to access the value of a property without having to use the object name
Example:
const person = { name: "John", age: 30 };
const {name} = person 

Typescript Cheat Sheet

dayvster.com
export default
Exports a variable, function, or class as the default export
Example:
export default function myFunction() {
  // Do something
}
// Import example
import myFunction from "./myFunction.ts"
Switch Case
switch
Creates a switch statement that can be used to check multiple values
Example:
switch (value) {
  case 1:
    // Do something
    break;
  case 2:
    // Do something
    break;
  default:
    // Do something
    break;
}
if
Creates an if statement that can be used to check a value
Example:
if (value === 1) {
  // Do something
}
Interface Example:
Example:
interface IPerson {
  name: string;
  age: number;
  isCool: boolean;
}
A Common practice is to prefix interfaces with an I
Class Example:
Example:
class Person {
  name: string;
  age: number;
  isCool: boolean;
  constructor(name: string, age: number, isCool: boolean) {
    this.name = name;
    this.age = age;
    this.isCool = isCool;
  }
}
Function Example:
Example:
function myFunction(param1: string, param2: number):number {
  return 200;
}
Thank you
Example:
Thank you for using this cheat sheet. If you have any
feedback, please let me know on Twitter @dayvsterdev
// Typescript Cheat Sheat
// Bringing it all together

export interface IPerson {
  name: string;
  age: number;
  isZombie: boolean;
  isJesus: boolean;
}

export const PrintPerson: (props: IPerson): void => {
  const { name, age, isZombie, isJesus } = props;
  console.log(`Name: ${name}`);
  console.log(`Age: ${age}`);
  if(isZombie) {
    console.log(`Eek it's a zombie`);
  } else {
    console.log(`Ah phew thought it was a zombie`);
  }
  if(isJesus) {
    console.log(`Jesus is here`);
  }
  if(isZombie && isJesus) {
    console.log(`Ah sick it's zombie jesus`);
  }
}

PrintPerson({ name: 'Jesus', age: 33, isZombie: false, isJesus: true });

export { PrintPerson, type IPerson }

export default PrintPerson

Explanation

What we do first is create an interface for our person. This interface will have the following properties:

Namestring
Agenumber
Is Zombieboolean
Is Jesusboolean

Next we create a function that will print out the person. Which will take in a person object and print out some info about it to the console

we use destructuring to get the properties from the person object

then we print out the name and age of the person

then we check if the person is a zombie and print out a message accordingly

then we check if the person is jesus and print out a message accordingly

then we check if the person is both a zombie and jesus and print out a message accordingly

Finally we call the function with a person object

We then export the function and interface so we can use them elsewhere

We also do a default export \o/