1

Sorry if the question sounds vague, basically;

I have a struct

type User = {
  name: string,
  age: number,
}

I specifically want to map the attributes into an array by doing

type UserKey = Array<keyof User>;

Later in the code, I want to map the variables which has UserKey type into something else, say into table headers

let tableHeaders: UserKey = ["name", "age"];

Everything is working as expected, but I want the array to have every key from the User so nothing is left out if the case maybe I add another attribute to User type in the future, the code will show error because not all key is covered in the array

let tableHeaders: UserKey = ["name", "age", "gender"]; // this will error as expected
let tableHeaders: UserKey = ["name"]; // this will not error, but I want this to error instead

Expectation:

let tableHeaders: UserKey = ["name"]; // ERROR, age key is required
  • 1
    See also: [Make sure array has all types from a union](https://stackoverflow.com/q/60131681/12299000) – kaya3 May 11 '22 at 17:15
  • 1
    TypeScript doesn't have the concept of an "exhaustive" array, there are only workarounds, like [this](https://tsplay.dev/NDkrVw). See answers to linked questions. – jcalz May 11 '22 at 17:15

0 Answers0