General

Start a vite project

method 1. Create Expo project

This seems to be the more pro way to do it Based on this YouTube tutorial

Folder Details

/src

Delete all folders and create an /src for the source code

method 2. https://vite.dev/guide/

method 3. Start from the React template (the Joachim / La Capsule way)

These are found in react.dev/learn

Using null, void, undefined

function doSomething(): void {
    console.log("Doing something...");
}

Functions: declare output

function add(a: number, b: number): number {
    return a + b;
}

or with arrow function

const add = (a: number, b: number): number => a + b;

Type alias

create a type with a name - I (the programmer) give it

function setLoggerTimeout(
    loggerCallback: (s1: string, s2: string) => string,
    delay: number
) {
    // do something
}

Or do this:

type LoggerCallback = (s1: string, s2: string) => string;

function setLoggerTimeout(loggerCallback: LoggerCallback, delay: number) {
    // do something
}

Importing types

import type { User, Post } from "./models";

Optional args

function newCharacter(name: string, role?: string): string {
    return `${name} is a ${role}`;
}
function newCharacter(name: string, role: string = "warrior"): string {
    return `${name} is a ${role}`;
}
function newCharacter(name: string, role = "warrior"): string {
    return `${name} is a ${role}`;
}

Literal types

Template Literal Types

type Class = "wizard" | "warrior" | "rogue";
type Race = "elf" | "human" | "dwarf";
type Hero = `Hero: ${Race} ${Class}`;
// Hero: elf wizard | Hero: elf warrior | Hero: elf rogue | Hero: human wizard | Hero: human warrior | Hero: human rogue | Hero: dwarf wizard | Hero: dwarf warrior | Hero: dwarf rogue

Heterogeneous Arrays

// TypeScript infers the type as (string | number)[]
let lightsaberStyles = [1, 2, "double", "shoto"];

function describe(style: string | number): string {
    console.log(`Wield ${style} lightsaber`);
}

lightsaberStyles.forEach(describe);
// Wield 1 lightsaber
// Wield 2 lightsaber
// Wield double lightsaber
// Wield shoto lightsaber

Object Literal Types

Type an object

type Saiyan = {
    name: string;
    power: number;
};

function logSaiyan(saiyan: Saiyan) {
    console.log(`${saiyan.name} has power level: ${saiyan.power}!`);
    // ...
}

Sets

TypeScript has a built-in type for sets, which are collections of unique values.

s = new Set([1, 2, 3, 3]);
// Set {1, 2, 3}

In TypeScript

const set = new Set<number>();
set.add(1);
set.add(2);
set.add(3);
set.add(3);
// Set {1, 2, 3}

Map

Maps are like objects but the keys can be anything - even a boolean if you want.

const map = new Map<string, number>();
map.set("one", 1);

Dynamic Keys

type UserMetrics = {
    [key: string]: number;
};

const metrics: UserMetrics = {
    wordsPerMinute: 50,
    errors: 2,
    timeOnPage: 120,
};

metrics["refreshRate"] = 60; // OK
metrics["theme"] = "dark"; // Error: Type 'string' is not assignable to type 'number'

Dynamic Default Properties

This is really good for users objects or headers where some properties are unknown until we make the request.

type FormData = {
    [field: string]: string;
    email: string;
    password: string;
};