/all-about-typescript

Types to Avanced Types, Interfaces, Generics, Decorators, and friends.

Primary LanguageTypeScript

Typescript

Warning before learning

  • TypeScript is Mostly Boring JavaScript

Why do we care about types

  • Types are used by the Typescript Compiler to analyze our code for errors
  • Types allow other engineers to understand what values are flowing around our codebase

Where do we use types

  • Everywhere in application, Every value you define

Type Annotation & Inference

  • Annotation(Developer Side): Code we add to tell Typescript what type of value a variable will refer to

    • Use when you declare a variable on one line then initialize it later
    • Use when you want a variable to have a type that can't be inferred
    • Use when a function returns the 'any' type and we need to clarify the value
  • Inference(Compiler Side): Typescript tries to figure out what type of value a variable refers to

    • Rely whenever necessary
      declaration       initialization
       ----^----          ----^----
      const color     =     'red'
    • if declaration and initialization are on the same line, Typescript will figure out the type of 'color' for us

Soundness and Completeness

  • They want to stop you from writing code that will raise type errors

    • This is called soundness: a sound type system will catch every type error that could happen at run-time. If you program type-checks, you'll never encounter a type error. No false negatives.
      Having array and map indexes return T | undefined would be more sound, but less complete.
    • When you get an element from an array, it might be undefined but Typescript doesn't track that.
  • They want to not prevent you from writing code that won't raise type errors

    • This is called completeness: a complete type system will accept any program that can't throw a type error(only yell type errors when there's a real legitimate type error) on run-time. No false positives.
  • They want to work well with existing idioms that predate the type system

    • Gradual Typing, this is where typescript falls and gets tricky

Gradual Typing

  • Typescript typing system typically lies somewhere in-between Static and Dynamic Typing

    let name = "John";

    If you hover over the variable name in the playground, you’ll see that TypeScript managed to associate name with the string variable. In fact, TypeScript provides type inference as well.

    let sum: (a:any, b:any) => any <- avoid any as much as possible

    In this case, TypeScript didn’t associate a particular type to either a , b or the return value of sum . The types of the sum function’s arguments or return could be anything. These types will be determined in runtime when executing the function. This is due to the '+' operator, which is not specific to any particular type in TypeScript or JavaScript.

    Conclusion:

    In a gradually-typed language such as TypeScript, some declarations will have their types checked during compile-time and others will have their types checked at run-time.

Variance

  • Variance describes rules for subtyping. Variance describes how the subtyping relationships of containers(like an Array, Map, or Function) relates to the inner type / their contents.
  • A type T is a subtype of a type U if anywhere that expects a U, you can use a T instead
  • Typescript generally uses covariance, which makes intuitive sense but is unsound when you mutate your arguments

Other Sources about typescript