-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Extending ES6 Map causes error - Constructor Map requires 'new' #10853
Comments
It seems I found a workaround for now with a static method to create new instances (could not get it to work with constructors). Not sure if there is any other implications of doing it this way.
|
Chrome (i.e. V8) does not provide an ES5 way to extend Map, so there's nothing TS can really do here. You can search for that error and find similar issues with babel, traceur, livescript, etc. Your workaround to use |
Can the following be used as workaround? It actually works with warnings.
|
No, sorry that creates another ton of errors. |
Please reopen the issue. Chrome V8 (v61) allows to extend map with no problem now, but typescript still does not work:
|
Just change your compilerOptions inside tsconfig.json new Map operator is a ES6 feature so you need to target your exported js to ES 6 Syntax Regards, |
@vtulin I haven't checked but you can probably return the Map from the constructor. Something like this: class myMap {
constructor(){
return new Map();
}
} The only problem is that typescript does not know this object actually inherits from the Map. I found a similar trick here: https://stackoverflow.com/a/40714458/532695 |
@Xample it is not correct to return something inside constructor since constructor is a VOID type function. |
@Stradivario no it's not a interface TypedConstructor {
new (parameter:string): TypedConstructor;
} It's the way it is in ES6, more reading |
What is the update about this issue? We should be able to extend Set and Map now :/ |
I know native types should be extendable since this PR #3516 , but I can't seem to get extending ES6 Map to work.
The following code
compiles correctly to:
But when I run it in chrome it gives an error
Uncaught TypeError: Constructor Map requires 'new'
When I run the original typescript code directly in the chrome console (which is possible because it's also valid ES6 and only uses functions supported by chrome) then it works correctly. So it should be possible?
Is there a way to extend ES6 Map from Typescript?
The text was updated successfully, but these errors were encountered: