'Why `keyof` doesn’t contribute to the reference of the original type? Does it lose track of the original symbol by design?

I'm wondering why renaming a property (in my IDE [VSCode] and in the TypeScript playground) in a type doesn't rename it in a string literal assigned to a keyof of that type.

References of this sort seem to come from TypeScript, rather than (just) from the IDE implementation, so I think it relates to how TypeScript keeps track of references internally.

(This TypeScript issue is very similar, supporting the idea that references like this are part of TypeScript.)

Suppose I start with this code:

type T1 = { a: true; b: true }
type T2 = { [T in keyof T1]: boolean }
type T3 = keyof T1

let t1: T1 = { a: true, b: true }
let t2: T2 = { a: true, b: true }
let t3: T3 = "a"

Then I highlight the a in T1 and do a rename operation in my IDE/ts playground, changing a to x. This is the resulting code:

type T1 = { x: true; b: true }
type T2 = { [T in keyof T1]: boolean }
type T3 = keyof T1

let t1: T1 = { x: true, b: true }
let t2: T2 = { x: true, b: true }
let t3: T3 = "a"

Notice how a changed to x in the type, and also in the object literals assigned to t1 and t2, but the string literal assigned to t3 didn't change from "a" to "x".

Mapped types also keep track. If I have:

type Merge<First extends object, Second extends object> = {
  [Token in keyof ({
    [K in keyof First]: First[K]
  } & {
    [K in keyof Second]: Second[K]
  })]: boolean
}

and then have this:

type M1 = { a: true; b: true }
type M2 = { b: true; c: true }
type M3 = Merge<M1, M2>
let m3: M3 = { a: true, b: true, c: true }

Renaming a to x in the IDE changes that to:

type M1 = { x: true; b: true }
type M2 = { b: true; c: true }
type M3 = Merge<M1, M2>
let m3: M3 = { x: true, b: true, c: true }

Notice how a was changed to x even in the object literal for M3.

Is this a language limitation? (Or even a bug?) Or this is how types should work in general?

Link to playground



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source