Skip to content

compiler option to emit interface symbols? #21479

Closed
@grofit

Description

@grofit

I have tried to find a request like this but have not found anything so feel free to close this one if it has already been requested elsewhere.

Feature Request

So with that out of the way is it possible that the TS team could look at adding the ability to output interfaces as symbols when compiling? currently interfaces are stripped out and empty files are left in their place, which is fine, but it would be good if we could tell the compiler to output a symbol into the files which would allow at runtime some referencing of the interface.

This is primarily useful for DI related scenarios but I am sure it would be useful for other use cases.

Current behavior:

export interface IFoo
{
   // innards not really important
}

outputs an empty file

Requested behavior:

export interface IFoo
{
   // innards not really important
}

outputs a file containing

/*module specific export stuffs*/ IFoo = Symbol.for("IFoo");

As you can see the interface name has been used as the symbol name and exposed variable.

You can tie this to an optional compiler arg like -emitInterfaceSymbols or something similar.

Example use cases

// ifoo.ts
export interface IFoo {}

// foo.ts
export class Foo implements IFoo {}

// some-di-config.ts
container.bind<IFoo>(IFoo).to(Foo); 

would output (syntax may not be 100% accurate, but hopefully shows the concept)

// ifoo.js
export const IFoo = Symbol.for("IFoo");

// foo.js
export function Foo {}

// some-di-config.js
container.bind(IFoo).to(Foo);

This would then allow at runtime for you to be able to reference the interface in a unique way without breaking any existing functionality.

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