Categories
Basic TypeScript

Everyday Types

https://www.typescriptlang.org/docs/handbook/2/everyday-types.html

  • Primitives:
    • string
    • number: no distinguish between int and float
    • boolean
  • Array:
    • T[] or Array<T>
    • [T] is Tuples
  • any: to skip type checking
  • Type Annotations:
    • type after variable
    • automatically inferred
  • Functions:
    • Parameter type: function greet(name: string)
    • Return type: function getNumber(): number
    • Return Promise type:
      async function getNumber(): Promise<number>
  • Object Types:
    • Value with properties
    • Optional properties: marked with ?
  • Union Types
    • A type formed from two or more other types
      id: number | string
    • An operation allowed only if valid for every member of the union -> type narrow
  • Interfaces:
    • Another way to name an object type
    • Key distinction is that a type cannot be re-opened to add new properties vs an interface which is always extendable
  • Type Assertion
    • Specify a type TS can’t know
    • Specify a more specific type
  • Literal Types
    • Difference between let and const
    • Combining literals -> unions
  • null and undefined
    • To signal absent or uninitialized value
    • strictNullChecks:
      • Need to test before using the method or property
    • Non-null assertion operator (Postfix !)
  • Less common primitives
    • bigint: very large integer
    • symbol: a globally unique reference

Leave a comment