Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

typescript array of possible object keys

const keyOptions = ["name", "gender", "address"] as const;
type TCustomKeys = { [key in typeof keyOptions[number]]: any };

const obj: TCustomKeys = {
  name: "James",
  gender: "M",
  address: "1 First Rd"
}; // no errors, and intellisense working
 
PREVIOUS NEXT
Tagged: #typescript #array #object #keys
ADD COMMENT
Topic
Name
1+7 =