Skip to main content

Typed ObjectType to ref any non-Primitive; as Record or Dictionary

Index Signature

TS Syntax to express Dictionary with arbitrary key map to value

Generic Object Types

TS annotate the following object

let point = {x: 10, y: 20}

let point: {x: number; y: number } = {x: 10, y: 20}

let point: {x: number; y: number} = {x: 10, y: 20}

object type values must be separated by semicolon

TS annotate destructured object properties

const profile = {
name: "bob",
age: 20,
coords: {
lat: 0,
lng: 15,
},
setAge(age: number): void {
this.age = age;
},
};

destructure and TS annotate age from profile object above: const { age }: { age: number } = profile;
destructure and TS annotate lat & lng from profile object above: const { coords: { lat, lng } }: { coords: { lat: number; lng: number } } = profile;

interface

Type to define Object shape & behavior blueprint

Object Literal Type

Type Annotation for Object Literal, use for inline anon Object pass as arg

Property Modifier

TS Syntax to specify property's Type, if optional? & if writable?

Typed Classes

object structure can be Record || Dictionary

References