TypeScript

Variables

/**
 * (1) x is a string, b/c we’ve initialized it
 */
let x = 'hello world'

/**
 * (2) reassignment is fine
 */
x = 'hello mars'

/**
 * (3) but if we try to change type
 */
x = 42 // 🚨 ERROR

/**
 * (4) let's look at const. The type is literally 'hello world'
 */
const y = 'hello world'

/**
 * This is called a 'string literal type'. y can never be reassigned since it's a const,
 * so we can regard it as only ever holding a value that's literally the string 'hello world'
 * and no other possible value
 */

Reference

All the code examples are coming from Mike's TypeScript Fundamentals Course

Types

Type is an easy way to refer to the different properties and functions that a value has.

A value in JS is anything that we can assign to a variable.

What can we assign to variables

  1. String

  2. Number

  3. Boolean

  4. null

  5. undefined

  6. Object {}

  7. Function

  8. Array

  9. Classes

Types in TS

Primitive Types

  • number

  • boolean

  • void

  • undefined

  • null

  • string

  • symbol

Object Types

  • functions

  • arrays

  • classes

  • objects

Type annotations and Type Inference

Type annotations is a code we add to tell TypeScript what type of value a variable will refer to. Type inference: TypeScript tries to figure out what type of value a variable refers to.

Annotations with Variables

Different type annotations

Type inference

If declaration and initialization are done on the same line, TypeScript will figure our the type for us

Last updated

Was this helpful?