-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
New operator should return object returned by constructor #38519
Comments
@yukulele As much as I support the request, I have my doubts that this would not break existing Typescript code. There may be code like this out in the open: class MyDateLikeClass {
constructor() {
const result = new Date();
(result as unknown as MyDateLikeClass).doX = MyDateLikeClass.prototype.doX.bind(result);
return result;
}
doX() {
}
}
function doSomething(with: MyDateLikeClass) {
}
doSomething(new MyDateLikeClass()); With your suggestion this code would break with Edit: class Foo {
constructor(): Promise<Foo> { // or maybe Promise<this> ?
return Promise.resolve().then(() => this);
}
}
const foo1 = await new Foo();
const foo2 = new Foo(); // foo2 is Promise<Foo> ; ts consider foo2 as Foo |
I really pity anyone working with code like this - |
@RyanCavanaugh I pity everyone who has to write declarations for code like this in current ts.
Obviously the first is plain wrong and the second is very tedious to write. |
@RyanCavanaugh this is how ecmascript works, but I admit it's neither the most well-known feature nor a commonly expected behavior... That said, I don't think it's that hard to work with that if the text editor is aware of the types. const foo = new Foo()
// editor knows `foo` type is `Foo | Bar`
foo.fooMethod() // Error: Property 'fooMethod' does not exist on type 'Bar'. ts(2339)
if(foo instanceOf Foo) {
foo.fooMethod() // ok
} |
|
@RyanCavanaugh It does have a certain kind of esthetics to it though 😄. However, we should try to stick to the topic of the ticket. |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new#Description |
Perhaps if you allow:
Then you have a way of disambiguating between constructors that return the instance, and constructors that return the promise of the instance. This shows how you can make use of the async constructor and extend it with some caveats involving private properties. |
@CMCDragonkai why only allow this specifically for Promises when we know that there is other code that would benefit from a generalized solution? |
Would love for that to be available, it would mean I can finally have async constructors for classes in TS. |
Search Terms
new, operator, constructor, return, object
Suggestion
in javascript, calling with the operator
new
a function that returns an object does not resolves to the created instance but to the object returned by the functionTypescript should do the same.
That could offer a better compliance with the ECMAScript standard.
Use Cases
This can for example allow to create async constructor (see examples)
Examples
//
obj1
andobj2
type should befoo | Date
Use case example: async constructor:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: