Static types

Structural type system only cares about the shape of the object, not to verify instance of particular object

Variables

Type inference

Just hover below line in .ts file

let age = 10;

type inference

  • TS automatically does type inference where ever possible (especially when direct value assigned).So no need to explicitly mention in such cases.
  • It's more useful for class variables and method params & return types ..etc

Constants

constants

  • so for constants, it's not data type - it's exact value as type (as they are readable)

Not initialized

let z; //considered as :any type
let z: number; //can specify explicitly

Static data types

Docs

  • Any //good to work with existing JavaScript
  • Boolean
  • Number
  • String
  • enum //use const
  • Array -> number[], string[], particularType[], any[]
  • ParticularObject/ interface
  • Void //normally return type of functions that do not return a value
  • Null, undefined
  • Never

Note: Use currently only bold items , also for readonly in classes better use const


Arrays

  • Default array type 'never', so array type must be changed at least to any to have values in it Array

    marks:number[] = [];
    marks:string[] = [];
    marks = [1]; //just hover on variable to see type inference

Tuples (TS_SPECIFIC*)

Array with fixed length

//check docs


Particular object

Object

When we define a object type, then object initialization needs all properties mandatory.

let cc: { houseNumber: number; streetName: string };
cc = {
streetName: "Fake Street",
houseNumber: 123
};
cc = { //throws error
houseNumber: 33
};
/**
* 🚨 Property 'streetName'
* 🚨 is missing in type '{ houseNumber: number; }'
* 🚨 but required in type '{ houseNumber: number; streetName: string; }'.
*/

Note : with in an object ‘;’ semi colon is used – so you can understand that it is not value but type (prettifier can add for you )

optional operator (?)

let dd: { houseNumber: number; streetName?: string };
dd = {
houseNumber: 33
};

Particular object with key access

(Index signature)

Index signatures describe how a type will respond to property access

interface PhoneNumberDict {
[numberName: string]:
undefined
| {
areaCode: number;
region: string;
};
}
let phoneNumObj:PhoneNumberDict = {
'9999999999':{
areaCode : 301,
region : 'INDIA'
},
'8888888888':{
areaCode : 302,
region : 'AMERICA'
}
}
phoneNumObj['8888888888'] //right
phoneNumObj['4444'] //won't throw error as undefined type specified

Interface

To re-use this type, create an interface

interface Address {
houseNumber: number;
streetName?: string;
}
//refer to it by name
let ee: Address = { houseNumber: 33 };

Note: ctrl + click a type to navigate to definition


UNION & INTERSECTION

(TS_SPECIFIC*)

Docs

Consider

export interface HasPhoneNumber {
name: string;
phone?: number;
}
export interface HasEmail {
name: string;
email?: string;
}

An intersection type combines multiple types into one

let otherContactInfo: HasEmail & HasPhoneNumber = {
// we _must_ initialize it to a shape that's assignable to HasEmail _and_ HasPhoneNumber
name: "Mike"
};
// we can access anything on _either_ type
// otherContactInfo.name;
// otherContactInfo.email;
// otherContactInfo.phone;

An Union type is either of types

let contactInfo: HasEmail | HasPhoneNumber =
Math.random() > 0.5
? {
// we can assign it to a HasPhoneNumber
name: "Mike",
phone: 3215551212
}
: {
// or a HasEmail
name: "Mike",
email: "mike@example.com"
};
// outside we can only access the .name property (that is only common in both types)
contactInfo.name;

Null and Undefined

Docs

Example : In cases where you want to pass in either a string or null or undefined (for functions)

let a:string | null | undefined;

Null

Never

type of values that never occur or function that never returns

// Function returning never must have unreachable end point
function error(message: string): never {
throw new Error(message);
}