Skip to content

Interfaces that extend 'Symbol' cannot be used as 'symbol' type directly #46956

Closed
@yusufkandemir

Description

@yusufkandemir

Bug Report

🔎 Search Terms

extends Symbol
extends Symbol index key
extending Symbol
InjectionKey
cannot be used as an index type

🕗 Version & Regression Information

⏯ Playground Link

Playground link with relevant code

💻 Code

// Reference for InjectionKey: https://github.com/vuejs/vue-next/blob/2d4f4554349db6b07027d0c626f56c48d0233f67/packages/runtime-core/src/apiInject.ts#L6
interface InjectionKey<T> extends Symbol {}

interface SomeInterface { foo: string }
const key: InjectionKey<SomeInterface> = Symbol()

const records: Record<string | symbol, any> = {};

records['a-string'];     // Works
records[Symbol()];       // Works
records[key];            // Doesn't work
records[key as symbol];  // Works

// Assignability tests
const _key1: symbol = key;           // Doesn't work
const _key2: symbol = key as symbol; // Works

// Casting tests

key as symbol; // Works        (Expected)
key as string; // Doesn't work (Expected)
key as number; // Doesn't work (Expected)
// ...

const sym = Symbol();

sym as symbol; // Works        (Expected)
sym as string; // Doesn't work (Expected)
sym as number; // Doesn't work (Expected)
// ...

🙁 Actual behavior

Interfaces that extend Symbol(e.g. InjectionKey) can't be used as symbol directly although they can be explicitly cast.

🙂 Expected behavior

Interfaces that extend Symbol can be used as symbol, which includes being used as index types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions