Skip to content
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

VS Code Declare Non-Global, Module-Based, Extension Methods #23341

Closed
Sharpiro opened this issue Apr 11, 2018 · 5 comments
Closed

VS Code Declare Non-Global, Module-Based, Extension Methods #23341

Sharpiro opened this issue Apr 11, 2018 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@Sharpiro
Copy link

TypeScript Version: 2.7.2

Search Terms: global modules declarations extensions VS Code

Code

extensions.ts:

declare global {
    interface Array<T> {
        peek(): T;
    }
}

Array.prototype.peek = function () {
    return this[this.length - 1];
}

export { }

main.ts:

//import "./extensions"

var data = [1, 2, 3]
var peek = data.peek()
console.log(peek)

Expected behavior:
I would like to have the ability to import extension methods and interfaces on a module basis. This would allow you to import an extension method interface and its implementation on an as-needed basis, or importing different implementations of an extension method. A good example of the desired behavior is in C# where you import a namespace to gain access to the extension method. Without that namespace, the extension method will not be present.
Actual behavior:
As far as I'm aware, extension methods in typescript must be declared on a global basis, meaning in a code editor like VS Code, you can only declare an extension method interface on a global level. This means you must ensure the implementation is loaded globally, or you will create an odd situation where you have a method in TypeScript, but not the implementation in JavaScript.

@Loridawn99
Copy link

Kudos everyone

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Apr 11, 2018
@RyanCavanaugh
Copy link
Member

I can't stress enough how bad of a plan this is. When you modify the prototype, you do so for everyone. And if you have "different implementations of an extension method", you really cannot predict which one was the last one in.

Our current support for declare global is all we intend to do here. Don't modify built-in prototypes. It's not worth the future pain you're going to incur on yourself or others.

@Sharpiro
Copy link
Author

It's odd to see Microsoft so anti-extension-functionality in TypeScript when it's not only highly used in C#, but is actually being pushed to "extension everything" in future language versions.

declare global seems to be more of a hack workaround imo.

Alright, this was just a request, thanks for the feedback

@RyanCavanaugh
Copy link
Member

@Sharpiro probably worth reading #9

@Sharpiro
Copy link
Author

Thanks for posting that, looks like this has been a heated debate for a long time

@microsoft microsoft locked and limited conversation to collaborators Jul 30, 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