Arkar Myat

TypeScript - Object values as type

Feb 19, 2024Arkar
Typescript

Types from both keys and values of object in Typescript

const CART_RESPONSE = {
  NO_CARTID: "missingCart",
  NO_LINEID: "missingLineId",
  NO_VARIANT: "missingVariantId",
  ERROR_ADDING: "Error adding item to cart",
  SUCCESS: "success",
} as const; // <- don't forgot const

type Keys = keyof typeof CART_RESPONSE;

export type CART_RESPONSE_VALUE = (typeof CART_RESPONSE)[Keys];