-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
Milestone
Description
TypeScript Version: 3.0.0-dev.201xxxxx
Search Terms: newable signature
Code
declare function construct<T>(ctor: new(...args: any[]) => T): T;
declare function construct<T>(ctor: Function): T;
class C {
constructor(public x: string) {};
}
const instance = construct(C);
instance.x;Expected behavior:
The first overload is chosen, instance is of type C, thus no error at instance.x.
Actual behavior:
Second overload is chosen, instance is of type {} (as per underspecified generics rules), error at instance.x because {} has no x.
Some odd things that I don't fully understand:
- only happens when we turn on
strictFunctionTypes - doesn't happen if C has no ctor arguments.
- if there is a single overload for the newable signature only, it is chosen fine.
Note this is based on real world code in angular typings.
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/8091b4653666317dc94a3ede9f84d3114c67db81/types/angular/index.d.ts#L1394
One workaround is to change typings not to use underspecified Function type.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug