DEV Community

ikechukwu1234
ikechukwu1234

Posted on

TYPESCRIPT

TypeScript is a syntactics superset of JavaScript which adds statics types.
TypeScript being "syntactics superset" means that it shares the same base syntax as JavaScript, but add something to it.

Why should I use TypeScript?

  • Compilation: it a compiler of code and code error, it has error detector.
  • It support OOP: it support the Object Oriented Programming Language
  • It supports type definition from existing JavaScript:it shows the data type while your JavaScript don't when typing or programming.

TypeScript Type system
1) any Type: is a super type of all type in TypeScript. it disable type checking and effectively allows all types to be used.
2) built in type: your string:"", number:1, null, undefined
3) user defined type: the user is the one to determine the types of data to be use in the application.

Type Assignment
when creating a variable in TypeScript, there are two main ways TypeScript assigns a type:
1) Explicit:writing out the type
e.g let firstname: string = "James";
Explicit type assignment are easier to read and more intentional
2) Implicit:TypeScript will "guess" the type,based on the assigned value
let firstname = "John";
Note:Having TypeScript "guess" the type of a value is called: infer.

Implicit assignment force TypeScript to infer the value.

Implicit type assignment are shorter, faster to type,and often used when developing and testing.

Error in Type Assignment
TypeScript will throw error if data types do not match.

TypeScript syntax for variable declaration
var identifier : Type = value;

Example of Type:any
let k: any = true;
k = "string";
Example of TypeScript Arrays
TypeScript has a specific syntax for typing arrays variable.
e.g: let fruit: (string| number|null|boolean)[] = ["mango", 80, 90,null,true,false,"Edwin","Gideon"];
Example of TypeScript Tuples
Tuples is a typed array with a pre-defined length and types for each index.
Tuples help or allow each element in the array to be a known type of value.
To defined a tuple,specify the type of each element in the array
e.g:let studentscore: [string,number,string,boolean,string] = ["John", 30,"jude",true,"I love coding"];
Example of TypeScript syntax of an object
e.g: let car:{
name: string;
color: string;
wheel: number;
year: number;
}={
name: "Benz";
color: "White";
wheel: 4;
year: 2022;
};

Top comments (0)