-
Notifications
You must be signed in to change notification settings - Fork 12.8k
express.d.ts simple usage broken in latest bits #11875
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
Comments
was broken in 8094b2c |
A even slimmer repro: var use: Overload;
use((req, res) => {}); // <--- error on this call to 'use'
interface Overload {
(handler: Arity1): void;
(handler: Arity2): void;
}
interface Arity1 {
(req: string): void;
}
interface Arity2 {
(req: number, res: string): void;
} The problem is unannotated parameter inference is not changed after first call of checkExpression. |
The problem here is in choosing which overload signature for contextual typing. function checkFunctionExpressionOrObjectLiteralMethod(node: FunctionExpression | MethodDeclaration, contextualMapper?: TypeMapper): Type {
// omitted....
if (mightFixTypeParameters || !(links.flags & NodeCheckFlags.ContextChecked)) {
const contextualSignature = getContextualSignature(node);
const contextChecked = !!(links.flags & NodeCheckFlags.ContextChecked);
if (mightFixTypeParameters || !contextChecked) {
links.flags |= NodeCheckFlags.ContextChecked;
if (contextualSignature) {
const signature = getSignaturesOfType(type, SignatureKind.Call)[0]; // <- problem here
if (contextSensitive) {
assignContextualParameterTypes(signature, contextualSignature, contextualMapper || identityMapper);
}
// omitted...
} The @ahejlsberg @sandersn Any idea about fixing this? I have thought about 1. give up contextual typing if in an overloaded call, or 2. reassign contextual parameter types in |
@HerringtonDarkholme I'm pretty sure that contextual typing does not apply when the contextual type is an overloaded call signature. So probably (1) is correct. Your minified repro illustrates this -- |
Yes, |
We shouldn't be applying contextual types to a function expression with greater parameter arity than the source overload. It's not a complete fix, but prevents breakage in cases where doing so is clearly wrong. |
@vladima can you take a look. |
I just want to confirm, should this code break in TS 2.0.6? It suffers from the same problem here. var use: Overload;
use(a => {}); // <--- error on this call to 'use'
interface Overload {
(handler: ReturnString): void;
(handler: ReturnVoid): void;
}
interface ReturnString {
(a: string): string;
}
interface ReturnVoid {
(a: number): void;
} |
Yes, it turns out the bug has been there a long time. @RyanCavanaugh thinks about 1.1 or so. We just noticed it now because a lot more things are contextually typed than before. |
@HerringtonDarkholme the behavior with your last sample is expected. There is no way at runtime for the caller to know what the type of the call back argument is, so what would What you would want to say is, and interface Overload {
(handler: AcceptsNumberOrString): void;
}
interface AcceptsNumberOrString {
(a: string): string;
(a: number): void;
} and you would implement it with a function that has two overloads. |
A basic Express 4 sample has errors with the latest bits. It appears to be a regression in TypeScript 2.1.
Below is how far I’ve managed to reduce the code into a standalone repro before heading home (from the definition currently in @types/express).
If I compile with the current bits in the “release-2.0.5” branch there is no error. This is specific to what is currently in “release-2.1”.
The text was updated successfully, but these errors were encountered: