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

Return type of 'super()' should be 'this', not 'void' #42298

Closed
rbuckton opened this issue Jan 12, 2021 · 9 comments
Closed

Return type of 'super()' should be 'this', not 'void' #42298

rbuckton opened this issue Jan 12, 2021 · 9 comments
Labels
Duplicate An existing issue was already created

Comments

@rbuckton
Copy link
Member

rbuckton commented Jan 12, 2021

Found when reviewing #29374. We currently treat the return type of super() as void, but it really should be this.

for(a; super();) {}
~~~~~~~
!!! error TS1345: An expression of type 'void' cannot be tested for truthiness.

This error is interesting because the result of super() is certainly not void. This isn't something you need to address, however.

Originally posted by @rbuckton in #29374 (comment)

@craigphicks
Copy link

Why should/must it be 'this'? I am not questioning that it could be this, as a matter of design choice. Does the current return type void cause any problems?

@rbuckton
Copy link
Member Author

Because that's the behavior of super() in a constructor in ES2015. When you call super(), it sets the this value:

class Base {}
class Derived extends Base {
  constructor() {
    let x = super();
    console.log(x === this);
  }
}
new Derived(); // logs: true

@MartinJohns
Copy link
Contributor

MartinJohns commented Jan 12, 2021

Maybe related? #38519

@MartinJohns
Copy link
Contributor

Duplicate of #37847.

@craigphicks
Copy link

Because that's the behavior of super() in a constructor in ES2015. When you call super(), it sets the this value:

class Base {}
class Derived extends Base {
  constructor() {
    let x = super();
    console.log(x === this);
  }
}
new Derived(); // logs: true

So the reason 'for it' would be so that JS code could pass the TS compiler.
Are there are examples where JS doesn't pass the compiler? I seem to remember so.

I can see reasons 'against it': x would be a duplicate of this and the compiler has to track that semantically. It's more confusing for a human reader to see two symbols referencing the same object.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Jan 12, 2021
@ExE-Boss
Copy link
Contributor

x would be a duplicate of this and the compiler has to track that semantically.

That’s already supported:

let x: this;

@LongTengDao
Copy link
Contributor

LongTengDao commented Jan 15, 2021

Why should/must it be 'this'? I am not questioning that it could be this, as a matter of design choice. Does the current return type void cause any problems?

class MyArray extends Array {
    constructor () { return super(); }
}

It's faster than default constructor.

@ExE-Boss
Copy link
Contributor

For Array, you might want to do:

class MyArray extends Array {
	constructor(length = 0) { return super(+length) }
}

to correctly support ArraySpeciesCreate with non‑0 length.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

7 participants