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

Better error messages for mixins #17504

Closed
ghost opened this issue Jul 29, 2017 · 5 comments
Closed

Better error messages for mixins #17504

ghost opened this issue Jul 29, 2017 · 5 comments
Assignees
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Fixed A PR has been merged for this issue

Comments

@ghost
Copy link

ghost commented Jul 29, 2017

TypeScript Version: nightly (2.5.0-dev.20170727)

Issue #4890, "Allow class to extend from a generic type parameter", is marked solved. So let's try it out:

function f<T extends new() => any>(C: T) {
	return class extends C {}
}

Huh? We get src/a.ts(2,23): error TS2507: Type 'T' is not a constructor function type..
That's not too intuitive since it worked fine with C: new() => any.

We actually need:

function g<T extends new(...args: any[]) => any>(C: T) {
	return class extends C {}
}

Maybe the error message should mention something about needing ...args: any[]?

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Jul 29, 2017

@andy-ms thank you! I didn't realize the signature had to be exactly new (...args: any[]) => any and was trying to use new (...args: {}[]) => any which currently fails with the not a constructor function type error.

@mhegazy mhegazy added Domain: Error Messages The issue relates to error messaging Bug A bug in TypeScript labels Aug 23, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Aug 23, 2017

@DanielRosenwasser recommendations?

@mhegazy mhegazy added this to the TypeScript 2.9 milestone Apr 12, 2018
@mhegazy mhegazy assigned weswigham and unassigned sandersn Apr 26, 2018
@mhegazy mhegazy modified the milestones: TypeScript 3.0, TypeScript 3.1 Jul 2, 2018
@DanielRosenwasser
Copy link
Member

Classes may only extend types bounded by constructors with a single rest parameter of type 'any[]' (e.g. 'new (...args: any[]) => {0}').

Related span:

Did you mean for '{0}' to be declared with type 'new (...args: any[]) => {1}'.

@ghost
Copy link
Author

ghost commented Aug 13, 2018

Correction: We actually actually need:

function g<T extends new(...args: any[]) => {}>(C: T) {
	return class extends C {}
}

Returning any is unsafe, see #26286

@thejohnfreeman
Copy link

People have been getting this wrong for over a year. Maybe TypeScript should add a constructor<T> built-in type?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

7 participants