TS is overly picky when declaring a class constructor type
See original GitHub issueTypeScript Version: 3.4.0-dev.20190202
Search Terms: class, extend, constructor, any[]
Code
type ClassConstructor = new(...args: any[]) => {}
function mixin<C extends ClassConstructor>(Class: C) {
return class extends Class {}
}
Expected behavior:
I should be able to replace ...args: any[]
with ...args: unknown[]
, or any other signature.
Actual behavior:
Error: Type 'C' is not a constructor function type. [2507]
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:8 (7 by maintainers)
Top Results From Across the Web
Class constructor type in typescript? - Stack Overflow
typeof Class is the type of the class constructor. It's preferable to the custom constructor type declaration because it processes static class ...
Read more >Documentation - Classes - TypeScript
Class constructors are very similar to functions. ... Constructors can't have type parameters - these belong on the outer class declaration, ...
Read more >Writing a constructor in TypeScript - LogRocket Blog
This reads weirdly, but it essentially means that the constructor isn't an instance type method. By instance type method, we're referring to a ......
Read more >Classes | Kotlin Documentation
The class declaration consists of the class name, the class header (specifying its type parameters, the primary constructor, and some other things), and...
Read more >TypeScript Basics 13 - Methods and constructors - YouTube
Access the full course here: https://javabrains.io/courses/typescript_basics Learn how to define methods in a TypeScript class.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Agree,
unknown[]
should be an acceptable substitute. Accepting PRs for an easy fix.@RyanCavanaugh I can look into that, but this problem seems much bigger than mixins and would have implications broadly. Rest parameters of type
unknown[]
are generally not compatible with arbitrary parameter types even if we are talking about a constraint. This currently fails understrictFunctionTypes
I’m not 100% sure it is necessarily a great idea to make the code above error free . It seems to be weaker as far as type safety goes. As we see in the code above the callback passed in accepts a
number
but it’s called insidetest
with astring
. I think the current behavior is better, don’t let a function with anumber
parameter be assigned to a function with anunknown
parameter, the implementing function can’t handleunknown
.While this is no worse than if we use
any[]
,unknown[]
would give a false sense of type-safety when no safety actually exists. Except for obeying theno-any
tslint rule not sure whyunknown
is better in this instance. At leastany
would function as a ‘💀💀 careful unsafe 💀💀’ warning, at least that’s the way I seeany
😃