Typescript 101

ยท

2 min read

Typescript is a superset of Javascript, that adds new features and type-safety. Js is dynamically typed whereas Ts is statically Js fails during run time, ts fails during development

IDE SETUP

  • To ease the manual reload of the page each time we compile the file we can convert this into a node project (npm init -y) and then do npm i --save-dev lite-server

  • Instead of manual compilation each time, we can trigger compilation using -- watch ie tsc app.ts --watch

  • To compile the entire project, we can run tsc --init once creating tsconfig.json we can exclude and include files by modifying this config file

  • Try Debugger for Chrome vs code extension

Basics

Types

  1. number - represents numeric values.

  2. string - Represents textual data.

  3. boolean - Represents true or false values.

  4. any - Represents a dynamic or untyped value.

  5. void - Represents the absence of a value.

  6. null - Represents a null value.

  7. undefined - Represents an undefined value.

  8. object - Represents non-primitive values. ex {age:30}

  9. Array - Represents an array of values.

  10. Tuple - Represents an array with a fixed number of elements where each element may be of a different type.

  11. Enum - Represents a set of named constant values.

  12. Function - Represents a JavaScript function.

  13. Interface - Defines the structure of an object.

  14. Class - Represents a blueprint for creating objects.

  15. Union - Represents a value that can be one of several types.

  16. Intersection - Represents a value that has all the properties of multiple types.

  17. Type Assertion - Allows you to tell the compiler the type of a variable when the type cannot be inferred.

  18. Type Alias - Allows you to create custom types.

  19. Literal Types - Represents a specific value, like "hello" or 42.

Type Assignment and Type Inference

NOTES:

ย