Skip to main content

Type

type keyword is used to create signature for objects, functions, arrays etc

type should be more preferred over interface

  1. Objects
type Person = {
name: string;
age: number;
color: "black" | "white";
};

// The color key can only have string value of "black" or "white"

const person: Person = {
name: "Rishabh",
age: 23,
color: "white",
};
  1. Functions
type FunctionType = (num: number) => number;

const alwaysEven: FunctionType = (num) => {
return num * 2;
};
type TFunction = (props: {
num: number,
display: (num: number) => void
}): void;