-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Make 'new.target' emit more precautions #15474
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
Comments
Technically we could do this - but I don't think it's worth it. TypeScript already gives you an error if you try calling your class (and in ES2015 it's an error to do so). Are people honestly going to be |
Yes, from a business app perspective it might not make sense, but I'm trying to see if it is possible to use the constructor system to both construct and reconstruct an instance for better performance. This is a common practice for game development, where using "new" is never a good idea, but rather a disposal pattern of creating and rebuilding disposed types. You might think I could just use a "create" static function, but no, that doesn't work, since derived types could not create a new parameter signature like you can do with constructors, so I'm stuck with constructors in TS (especially if I want to use get/set properties on a class, which is not supported anywhere else). See SO: http://stackoverflow.com/questions/18364175/best-practices-for-reducing-garbage-collector-activity-in-javascript (quote: "3 TravisJ - It's not at all uncommon in game frameworks.") And here: http://buildnewgames.com/garbage-collector-friendly-code/#avoid-creating-objects Add Construct 2 (most popular HTML5 game dev tool): https://www.scirra.com/blog/76/how-to-write-low-garbage-real-time-javascript (quote: "First of all, most obviously, the new keyword indicates an allocation, e.g. new Foo(). Where possible, try to create the object on startup, and simply re-use the same object for as long as possible.") It doesn't break anything, and in fact makes it better, so I couldn't see why not? (nothing wrong with making things more reliable) :) I'd like to see Typescript better support the gaming community as well with better support for things like callable constructors (yes, I know there's already a long discussion on this #183), and so on, otherwise the workarounds make for very poor looking end user frameworks. |
Here is an example of what I mean: https://goo.gl/uf0teb
This is the closest I can get. The actual code is a bit more complex, but the jist of it is above. The |
What it sounds like is that you are banking on the fact that TypeScript will allow your class to be callable without erroring. You should probably not be making that assumption. We've had people rely on using |
I can't see why this would break. The constructors are just a chain of calls. There's no harm in me creating an "isnew" parameter, then making sure users of a framework put all the "init" logic in the constructor. As far as inheritance goes, only constructors can have different parameters, so there's not much choice anyhow. |
Ah, nevermind. I just found out with more poking around (you sort of alluded to it) that there are specialized functions in ES6, and this direction is bad. It fails in Chrome with |
"new.target" has been addressed in a number of issues and closed off as a new ES feature that is now "supported" since TS 2.2. Rather than open yet another issue on new.target (and as this issue seems to be "in discussion") may I add that it is not apparent how this newly supported feature can be actually be used both write and consume a function supporting it in TypeScript. The following code illustrates what I have been trying to do (with various attempts using interface definitions) but to no avail.
Note that by uncommenting out |
Update .. resorting to an cast hack & const function assignment circumvents all compile errors though somehow doesn't feel like it's the right thing to do. This code works as commented:
|
That would be fine for simple things, and was what I did originally. This does not work as static methods to a class to be inherited by other types - since classes cannot redefine the function call signature of static functions in the base type (which is where a "new" or similar modifier of sorts might come in handy). Ideally, it would be nice to call a simple and common |
My example of a callable pattern, which better works with inheritance, is as follows:
Obviously, this is an idea only, and not complete. In the factory world of things, derived types would have to call the base types to initialize the instance if pulled from a cache (this pattern is common for JS games to reduce GC hits). All this could easily be simplified if I could just force the change of inherited static member signatures. |
Sorry, the "In Discussion" refers to the team discussing an amended Let's keep this issue open to people who need an amended |
Uh oh!
There was an error while loading. Please reload this page.
I noticed that the polyfill for
new.target
...... outputs to this:
That seems dangerous to me, since this is always a true condition for older browsers that don't yet support
new.target
. Does it not make more sense to create an output such as this?:The following code fails to work as expected in Chrome v56.0.2924.87 (output is "Good." in call cases):
The text was updated successfully, but these errors were encountered: