Skip to content

compiler option to emit interface symbols? #21479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
grofit opened this issue Jan 30, 2018 · 2 comments
Closed

compiler option to emit interface symbols? #21479

grofit opened this issue Jan 30, 2018 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@grofit
Copy link

grofit commented Jan 30, 2018

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.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 30, 2018

Types are design-time construct only. It is one of the TS design goals to provide an erasable type system.

You can find related discussion in #3628

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jan 30, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants