Object
Object Type
//we can define the type of an object before using it
type Person={
first:string,
last:string,
age:number
}
const printName=(person:Person)=>{
console.log("Hi!"+person.first);
}
Nested object
type Person={
first:string,
last:string,
age:number,
address:{
street:string,
city:string,
postal:string
}
}
const printCity = (person:Person)=>{
console.log("You live in "+person.address.city);
}
Optional Property
//where one or more properties can be optional
type Point={
x:number,
y:number,
z?:number
}
const myLocation : Point = {x:5,y:1};
const = calculate(point:Point)=>{
return point.x*point.y;
}
Readonly Modifier
//cannot be changed once it is set
type User={
readonly id:string,
username:string
}
const user:User ={id:111,username:'tom'}
user.id = 222; //-----this will not work
Intersection Type
type Person={name:string}
type Pet={breed:string}
type PersonwithPet = Person & Pet;
const a :PersonwithPet = {name:"john", pet:"collie"}