Repository: TypeScript
Issue: Symbols produced by an alias of the Symbol constructor cannot be used as object keys in types/interfaces/classes, etc.
Labels: Suggestion,Awaiting More Feedback

# Bug Report

Symbols returned by an alias of the global Symbol constructor cannot be used as objects keys. See this [playground example](https://www.typescriptlang.org/play?ts=4.9.5&ssl=14&ssc=1&pln=13&pc=20#code/MYewdgzgLgBAyjAvPAngWwEYgDYG4CwAUEaJLHOltgPoDWApigIJLwAUAlAYadKpjjqMAQqziduvcpUEMUAYTESiRKCgAO9GABUN9AOoBLKAAsKA7AGlGrAN5EYMANrmqQ5gF0AXDDABXTHoAJwdnV1kRb18AjGDQlxkaOXko-0CQwhgAX24HKRg0AENTVgBZYpNcnnA+ADNWNhQfNNigjmaY4KQAPhh7TJhselgAD1Yi0wA6QowIRq5QseQJk0m0QzA2EYAaGABaAFYFgaWCisnsEABzLY5QoOG-ILAYEe4slUJatj2ARgAGY5AA).

In and of itself this is not a bug per se, but it can result in increase bundle size when using a minifier such as terser. Terser does not mangle global names such as  Math, Symbol, etc. (see: https://github.com/terser/terser/issues/1337), so a common trick to avoid them reappearing in minified code is to assign the global name to a local variable.

For instance, consider the following typescript code that uses the globals Symbol and Math: [playground link](https://www.typescriptlang.org/play?ts=4.9.5&ssl=20&ssc=1&pln=1&pc=1#code/LAKAxg9gdgzgLgAgMoE8C2AjCAbA+gawFMUBBBAXmXS2wAoBKAblElkVUxwOICEKrOdJi2jwBNbigDC-DjQbMQoOCgAOhBABU1hAOoBLOAAs5OANLF+Ab1AIEAbVN4ipALoAuBFACumQgCdbBydJHg8vXwwAoMdqLhcpcJ8-QJAEAF9FETYEADN+WhRPZKj-emLIgIoAPgQbNIRsQkQAD34AWQBDYwA6TowYQuEGtsou3rR9KFoWgBoEAFoAVmG7UYRxox7sCABzGfog-2bvfygEFsV00FBc2gWARgAGYaA)

```
const Symbol_keyA = Symbol();
const Symbol_keyB = Symbol();
const Symbol_keyC = Symbol();

type TypeWithSymbolKey = {
  [Symbol_keyA]: number
  [Symbol_keyB]: number
  [Symbol_keyC]: number
 };

const f = (y: number): number => {
  let x = Math.abs(y);
  x = Math.min(x, -5);
  x = Math.log(x)
  return x;
}

f(-10);
```
when compiled to javascript by TS it generates the following output:

```
"use strict";
const Symbol_keyA = Symbol();
const Symbol_keyB = Symbol();
const Symbol_keyC = Symbol();
const f = (y) => {
    let x = Math.abs(y);
    x = Math.min(x, -5);
    x = Math.log(x);
    return x;
};
f(-10);
```
and when minified using terser you get the following output (you can try this using https://try.terser.org):

```
Symbol(),Symbol(),Symbol();(l=>{let a=Math.abs(l);a=Math.min(a,-5),a=Math.log(a)})(-10);
```


Now consider this minor tweak to the original code, which aliases the global Math name to a local variable: [playground link](https://www.typescriptlang.org/play?ts=4.9.5&ssl=22&ssc=1&pln=2&pc=2#code/LAKAxg9gdgzgLgAgMoE8C2AjCAbA+gawFMUBBBAXmXS2wAoBKAblElkVUxwOICEKrOdJi2jwBNbigDC-DjQbMQoOCgAOhBABU1hAOoBLOAAs5OANLF+Ab1AIEAbVN4ipALoAuBFACumQgCdbBydJHg8vXwwAoMdqLhcpcJ8-QJAEAF9FW1YxNABDY34AWQKjLPBRRAAzfloUT2So-3oGyICKAD4EGzSEbEJEAA9+fOMAOjyMGDrhXuHKUaMxtH0oWkGAGgQAWgBWWbt5hEWx7AgAc3X6IP8B738oBEHFdNBQKtptgEYABlmgA
)

```

const Symbol_keyA = Symbol();
const Symbol_keyB = Symbol();
const Symbol_keyC = Symbol();

type TypeWithSymbolKey = {
  [Symbol_keyA]: number
  [Symbol_keyB]: number
  [Symbol_keyC]: number
 };

 const math = Math;

const f = (y: number): number => {
  let x = math.abs(y);
  x = math.min(x, -5);
  x = math.log(x)
  return x;
}

f(-10);
```
when compiled to javascript by TS it generates the following output:

```
"use strict";
const Symbol_keyA = Symbol();
const Symbol_keyB = Symbol();
const Symbol_keyC = Symbol();
const math = Math;
const f = (y) => {
    let x = math.abs(y);
    x = math.min(x, -5);
    x = math.log(x);
    return x;
};
f(-10);
```

which terser can minify to:

```
Symbol(),Symbol(),Symbol();const l=Math;(o=>{let b=l.abs(o);b=l.min(b,-5),b=l.log(b)})(-10);
```

Obviously in this small example there isn't a significant difference in minified size, but in a large library that includes hundreds of calls to a top level name, this reduction can be significant. Its not uncommon to re-export math functions from an internal module to enable this type of minified output when one is focused on reducing output size.

Unfortunately, for libraries written in TS that choose to use symbols for object keys, class methods, etc. the resulting output can become littered with invocations of the Symbol constructor and there is no typesafe way in typescript to work around this.

### 🔎 Search Terms

Symbol

### 🕗 Version & Regression Information

4.95

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Symbol

### ⏯ Playground Link

Symbols returned from alias to Symbol not working as keys: [playground example](https://www.typescriptlang.org/play?ts=4.9.5&ssl=14&ssc=1&pln=13&pc=20#code/MYewdgzgLgBAyjAvPAngWwEYgDYG4CwAUEaJLHOltgPoDWApigIJLwAUAlAYadKpjjqMAQqziduvcpUEMUAYTESiRKCgAO9GABUN9AOoBLKAAsKA7AGlGrAN5EYMANrmqQ5gF0AXDDABXTHoAJwdnV1kRb18AjGDQlxkaOXko-0CQwhgAX24HKRg0AENTVgBZYpNcnnA+ADNWNhQfNNigjmaY4KQAPhh7TJhselgAD1Yi0wA6QowIRq5QseQJk0m0QzA2EYAaGABaAFYFgaWCisnsEABzLY5QoOG-ILAYEe4slUJatj2ARgAGY5AA)

### 💻 Code


### 🙁 Actual behavior

Symbols returned by aliases of the global Symbol constructor are not unique and cannot be used as keys in interfaces and types.

### 🙂 Expected behavior

Symbols returned by aliases of the global Symbol constructor should be unique symbols usable as keys in interfaces and types.