1

I am using the following code.

type PersonRow = {
  name: string;
  email: string;
  phone_number: string;
  age: number;
}

const personColumnNames: (keyof Omit<PersonRow, 'age'>)[] = [
  'name',
  'email',
  'phone_number'
];

However I want to get an error when I remove phone_number from the personColumnNames (in case a column is ever added to the PersonRow type).

type PersonRow = {
  name: string;
  email: string;
  phone_number: string;
  age: number;
}

const personColumnNames: (keyof Omit<PersonRow, 'age'>)[] = [
  'name',
  'email'
];

Edit:

Here is a hacky way I found but this still seems less than ideal.

const personColumnNames = Object.keys(((): { [key in keyof Omit<PersonRow, 'age'>]: null } => ({
  name: null,
  email: null,
  phone_number: null,
}))())
TheAschr
  • 899
  • 2
  • 6
  • 19
  • So the only valid value would be an array with three items (`name, email, phone_number`)? Would order matter? – Alex Wayne Dec 27 '19 at 19:41
  • Yes. Order does not matter. – TheAschr Dec 27 '19 at 19:42
  • Does [this](https://www.typescriptlang.org/play/#code/C4TwDgpgBAChBOBnA9gOwErIO5QLxQG8AoKUqVAQwFsIAuKRYeAS1QHMBuEsiKi5gDb1GLdlzJQwACzQQA+qgCuVAEYJhTVp26kKbOuWVr4XAL5EiAYzSNJCFKgDCyActQA5ahER4oAbR0oAHJKGiCAGkCg3n4BCKgAegSoayoaVGAoZEVM4GQGCGgqZkRELSjpWQUjBAjApOCpfiRQAQggxOTU9MzWKDyC6AgADyYKIgBdKAofa1RGLiJQSCgAWRzFCgEBEABRUYhUABNEAB4AFSgR4EOTqABVcIerg+OfADUn999zgD5fABuyGYR0Wy2gcCQaGcrioHi8iAAkogAPIAawoIF862Am22e1eJ1OgTREBAyAAZlAUcVgKdIQ5MFgnkE9O1fk8Ggh4Mh4FApAgIAB+KDFUrlCTgyl2KFOFxuTw0RB+JSqBBTLnwHl8gXwYUvMZEX4cTrkfLc3ki5AYkAAQgsQA) work for you? If so I'll make an answer. – jcalz Dec 28 '19 at 03:29
  • Possible duplicate of [Typescript - Create an exhaustive Tuple type from another type](https://stackoverflow.com/questions/58167616/typescript-create-an-exhaustive-tuple-type-from-another-type) – jcalz Dec 28 '19 at 03:31
  • Possible duplicate of [Enforce that an array is exhaustive over a union type](https://stackoverflow.com/questions/55265679/enforce-that-an-array-is-exhaustive-over-a-union-type) – jcalz Dec 28 '19 at 03:32
  • Possible duplicate of [What's the canonical way of checking for exhaustive keys union](https://stackoverflow.com/questions/53178218/whats-the-canonical-way-of-checking-for-exhaustive-keys-union) – jcalz Dec 28 '19 at 03:34

1 Answers1

1

For typing an array with Typescript, you have two options.

  1. The common SomeType[] which says "I have an array, and each item in that array is SomeType. This won't work for you because there is no way to type the length, and each item cannot be dependent on any other item.

  2. A tuple: [TypeA, TypeB, TypeC]. This represent an array of fixed length, where the item type at each index is known. This won't work for you because the order matters. I also do not believe you can create this type with a dynamic length.

So do I not believe there is a way to do what you want. And without knowing how you are planning to use this array, it's hard to suggest an alternative.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337