Object type annotations describe which properties a value should carry.

object shape An object type lists property names and their value types.

Object Shapes

plannedMinutes
objects.ts
Replay: real traced execution (multi-file project)
type Course = {
    title: string;
    minutes: number;
    published: boolean;
};

const plannedMinutes: number = 40;

const course: Course = {
    title: "TypeScript basics",
    minutes: plannedMinutes,
    published: true
};

const status: string = course.published ? "ready" : "draft";
const hours: number = course.minutes / 60;

console.log(`${course.title}: ${status}`);
console.log(`hours=${hours}`);
type Course = {
    title: string;
    minutes: number;
    published: boolean;
};

const plannedMinutes: number = 25;

const course: Course = {
    title: "TypeScript basics",
    minutes: plannedMinutes,
    published: true
};

const status: string = course.published ? "ready" : "draft";
const hours: number = course.minutes / 60;

console.log(`${course.title}: ${status}`);
console.log(`hours=${hours}`);
type Course = {
    title: string;
    minutes: number;
    published: boolean;
};

const plannedMinutes: number = 55;

const course: Course = {
    title: "TypeScript basics",
    minutes: plannedMinutes,
    published: true
};

const status: string = course.published ? "ready" : "draft";
const hours: number = course.minutes / 60;

console.log(`${course.title}: ${status}`);
console.log(`hours=${hours}`);
  1. plannedMinutes ← 40, course ← [object Object], status ← ready

    4    published: boolean;5};67const plannedMinutes→ 40: number = 40;  //@plannedMinutes=25, 5589const course→ [object Object]: Course = {10    title: "TypeScript basics",11    minutes: plannedMinutes40,12    published: true13};1415const status→ ready: string = course.publishedtrue ? "ready" : "draft";16const hours→ 0.6666666666666666: number = course.minutes→ 40 / 60;1718console.log(`${course.titleTypeScript basics}: ${statusready}`);19console.log(`hours=${hours0.6666666666666666}`);
    outputTypeScript basics: ready
    hours=0.6666666666666666
  1. plannedMinutes ← 25, course ← [object Object], status ← ready

    4    published: boolean;5};67const plannedMinutes→ 25: number = 25;89const course→ [object Object]: Course = {10    title: "TypeScript basics",11    minutes: plannedMinutes25,12    published: true13};1415const status→ ready: string = course.publishedtrue ? "ready" : "draft";16const hours→ 0.4166666666666667: number = course.minutes→ 25 / 60;1718console.log(`${course.titleTypeScript basics}: ${statusready}`);19console.log(`hours=${hours0.4166666666666667}`);
    outputTypeScript basics: ready
    hours=0.4166666666666667
  1. plannedMinutes ← 55, course ← [object Object], status ← ready

    4    published: boolean;5};67const plannedMinutes→ 55: number = 55;89const course→ [object Object]: Course = {10    title: "TypeScript basics",11    minutes: plannedMinutes55,12    published: true13};1415const status→ ready: string = course.publishedtrue ? "ready" : "draft";16const hours→ 0.9166666666666666: number = course.minutes→ 55 / 60;1718console.log(`${course.titleTypeScript basics}: ${statusready}`);19console.log(`hours=${hours0.9166666666666666}`);
    outputTypeScript basics: ready
    hours=0.9166666666666666

Follow the Shape

  1. Course has title, minutes, and published fields.
  2. plannedMinutes starts at 40.
  3. The course title is TypeScript basics.
  4. published is true, so status becomes ready.
  5. hours is 40 / 60, so the program prints hours=0.6666666666666666. | plannedMinutes | status | hours | | --- | --- | --- | | 25 | ready | 0.4166666666666667 | | 40 | ready | 0.6666666666666666 | | 55 | ready | 0.9166666666666666 |

Exercise: objects.ts

Reproduce TypeScript basics: ready and hours=0.6666666666666666, then use the pinned plannedMinutes values to predict each hours value.