TypeScript Types

Type Annotations

course = Web Development
credits = 4
isOnline = false
topics = HTML, CSS, JavaScript
section = CS5610, seats = 40

Object Types

CS5610 Web Development: Full stack web apps
CS5800 Algorithms: no description

Interfaces

Alice Wonderland (001234567) is in CS5610-02
Bob Builder works in WVH 310

Union and Literal Types

role = FACULTY
courseId = RS101, userId = 123
roles = STUDENT | FACULTY | TA

Enums

grade = A
day = 2 (Wednesday)
status = OPEN

Function Types

add(2, 3) = 5
fullName("Alice") = Alice
fullName("Alice", "Wonderland") = Alice Wonderland
greet() = Hello World
multiply(4, 5) = 20

Typing React Props

CS5610 (4 credits): Web Development
CS5800 (3 credits): Algorithms

Null, Undefined, and Narrowing

describe(null) = nothing
describe(7) = the number 7.0
describe("hi") = the text "HI"
alice.address?.city = Boston
bob.address?.city ?? "unknown" = unknown
nickname ?? "none" = none

any vs unknown

anything.toUpperCase() = HELLO
lengthOf("hello") = 5
lengthOf([1, 2, 3]) = 3
lengthOf(42) = 0
parsed name = Alice

Utility Types

updated.name = Web Development
fields = name: Web Development, number: CS5610, credits: 4
enrollment.CS5610 = 120, enrollment.CS5800 = 85