Typescript Basics

Table of Contents

  • Why Type Check?
  • Using typescript in Visual Code
  • Working with Objects
  • Working with Arrays
  • Converting JS files to TS files

Why Type Check?

Why should projects be using typescript?

Using TypeScript in Visual Code

Working with Objects

Interfaces

An interface declaration is another way to name an object type:

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

Setting up an Interface

// interface - working with objects
interface ObjectType {
  var1: number;
  var2: Array<boolean>;
}

Using the Interface to check the object type.

var data6:ObjectType = {
    var1: 2,
    var2: [true, true, false]
}

var data7:ObjectType = {
    var1: "d",
    var2: 234
};
var data8:ObjectType = {
    var1: 8,
    var2: [true]
}

Working with Arrays

<diagram of array type changing and the different errors>

var data4:Array<string>;
//works
data4 = ['h','d','d','v'];
//error
data4 = [1,2,3,4];

<examples of code used in production>

Converting JS files to TS files

Recent Posts