1

Given the following object:

const obj = { a: 1, b: 2, c: 3 };

I would like to build a type that returns:

type ObjKeysAsArray<T> = ?; // ObjKeysAsArray<typeof obj>: ["a", "b", "c"]

I thought about (keyof typeof obj)[], but it would return ("a" | "b" | "c")[], which is not what I want.

Eliya Cohen
  • 10,716
  • 13
  • 59
  • 116
  • 2
    The type system does not observe property order (e.g., the type `{a: number, b: number, c: number}` is the same as `{b: number, a: number, c: number}`) and union types are also not observably ordered. So there is no way to get an ordered tuple type from `keyof typeof obj` that's guaranteed to give you the same order as `Object.keys(obj)`. – jcalz Jul 18 '22 at 18:33

0 Answers0