"a" | "b"
and keyof ObjType1
refer to the same type within that specific code snippet.
In a similar scenario, the union type was not explicitly provided in the second instance.
The type keyof ObjType1
is essentially a union type. It serves as an alternative label¹ for "a" | "b"
, with both terms representing identical concepts (unless the definitions of ObjType1
or obj
are modified).
The primary distinction lies in the terminology utilized by TypeScript for display purposes (such as conveying information to IDEs via the language server process). However, TypeScript's type system typically emphasizes the structure of types rather than their names or aliases (aside from certain scenarios like declaration merging for interfaces). The system prioritizes the shape of types over nomenclature.
Regarding the choice of naming conventions in TypeScript between keyof ObjType1
and
"a" | "b"</code in the second circumstance, it likely aims to offer more informative insights regarding the origin of the type's definition. Opting for <code>keyof typeof obj
in the initial example would have also enhanced clarity compared to simply using
"a" | "b"
, although this may require understanding that
obj
pertains to a runtime identifier instead of a type.
¹ Alternatively, one could consider using the term label instead of an alias.