You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot use 'new' error on intersection type with constructor #17388
classX{}declareinterfaceConstructorAnyArgs<TClass>{new(...args: any[]): TClass}functionfoo(): ConstructorAnyArgs<X>{return}functionboo(): ConstructorAnyArgs<X>&{superclass: any}{return}letY=foo()lety=newY()letZ=boo()letz=newZ()// Cannot use 'new' with an expression whose type lacks a call or construct signature.
I have identified a problem when using intersection types in TypeScript...
I have three type aliases:
Prototype<T> - expresses an object/class that has a prototype property.
DefaultCtor<T> - expresses an object/class with a default constructor.
ParameterizedCtor<T> expresses an object/class with a parameterized constructor.
I have tried these intersection permutations:
Prototype<T> & DefaultCtor<T> - works fine.
Prototype<T> & ParameterizedCtor<T> - raises a compiler error.
Example
type Prototype<T> = {
prototype: T;
}
type DefaultCtor<T> = {
new(): T;
}
type ParameterizedCtor<T> = {
new(...args: any[]): T
}
function makeDefault<T>(ctor: Prototype<T> & DefaultCtor<T>): T {
return new ctor();
}
function makeWithArgs<T>(ctor: Prototype<T> & ParameterizedCtor<T>, ...args: any[]): T {
return new ctor(...args);
// ERROR: Cannot use 'new' with an expression whose type lacks a call or construct signature.
}
Error
The error occurs with the Prototype<T> & ParameterizedCtor<T> intersection:
Cannot use 'new' with an expression whose type lacks a call or
construct signature.
Why does the TypeScript compiler recognise the Prototype<T> & DefaultCtor<T> intersection type as having a constructor, but not the Prototype<T> & ParameterizedCtor<T> intersection type?
myInvalidFunc = (A: __example.A) => {
// [ts] Cannot use 'new' with an expression whose type lacks a call or construct signature.
const a: __example.A = new A();
}
Activity
RyanCavanaugh commentedon Jul 25, 2017
This regressed between 2.1 and 2.2
MrMatthewLayton commentedon Mar 27, 2018
Mohamed Hegazy (@mhegazy) Ryan Cavanaugh (@RyanCavanaugh)
Adding this from StackOverflow for completeness
I have identified a problem when using intersection types in TypeScript...
I have three type aliases:
Prototype<T>- expresses an object/class that has aprototypeproperty.DefaultCtor<T>- expresses an object/class with a default constructor.ParameterizedCtor<T>expresses an object/class with a parameterized constructor.I have tried these intersection permutations:
Prototype<T> & DefaultCtor<T>- works fine.Prototype<T> & ParameterizedCtor<T>- raises a compiler error.Example
Try it in the Playground
Error
The error occurs with the
Prototype<T> & ParameterizedCtor<T>intersection:Why does the TypeScript compiler recognise the
Prototype<T> & DefaultCtor<T>intersection type as having a constructor, but not thePrototype<T> & ParameterizedCtor<T>intersection type?gssjr commentedon Jun 15, 2018
I may be running into this as well. Here's my example:
weswigham commentedon Jun 15, 2018
This is correct.
__example.Aisn't a constructor.1 remaining item
Fix mixin logic to preserve at least one constructor type even when the
mattmccutchen commentedon Oct 11, 2018
A silly mistake in the logic for handling mixin classes. Fix is in #27701.
Fix mixin logic to preserve at least one constructor type even when the
Fix mixin logic to preserve at least one constructor type even when t…