get type of object typescript code example

Example 1: typescript type object

type Dictionary = {
  [key: string]: any
}

Example 2: typescript check type of variable

if (fooOrBar instanceof Foo){
  // TypeScript now knows that `fooOrBar` is `Foo`
}

Example 3: typescript get type

if (typeof abc === "number") {
    // do something
}

Example 4: typescript get type of object property

type Data = {
  value: number;
  text: string;
};
type textProperty = Data["text"]; //string

//OR
const data = {
  value: 123,
  text: 'hello'
};
type textProperty = typeof data["text"]; //string

Example 5: typescript type of object values

const data = {
  value: 123,
  text: 'text'
};
type Data = typeof data;