Skip to content

Latest commit

 

History

History
104 lines (76 loc) · 1.52 KB

File metadata and controls

104 lines (76 loc) · 1.52 KB

TypeScript object type

Typescript object type represents all values that aren't primitive types.

Primitive types are:

  • string
  • number
  • boolean
  • null
  • undefined
  • symbol

Examples

Declaring an object type

let person: object;

person = {
  name: "bob",
  age: 65,
};

console.log(person);

Output:

{
name: "bob",
age: 65
}

An object type can not be ressigned to a primitive type. This will result in an error.


Explicity asigning properties to a person object.

let person: {
  name: string;
  age: number;
};

person = {
  name: "dan",
  age: 45,
};

Here we start by defining the person object and its properties. Then we assign the person object to a literal object with described properties.

required properties:

name and age are required properties.

function printPerson(person: { name: string; age: number }) {
  console.log(`Name: ${person.name}`);
  console.log(`Age: ${person.age}`);
}

printPerson({ name: "bob", age: 100 });
printPerson({ name: "Dan", age: 45 });

Output:

    Name: bob
    Age: 100
    Name: Dan
    Age: 45

Optional properties:

Use ? to make a property optional. age is optional.

function printPerson(person: { name: string; age?: number }) {
  console.log(`Name: ${person.name}`);
  console.log(`Age: ${person.age}`);
}

printPerson({ name: "bob", age: 100 });
printPerson({ name: "Dan" });

Output:

    Name: bob
    Age: 100
    Name: Dan
    Age: undefined