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.

1
  • 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). Commented Jul 18, 2022 at 18:33

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.