Skip to content

Make function properties context-sensitive based on their return statements #50903

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18735,8 +18735,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function hasContextSensitiveReturnExpression(node: FunctionLikeDeclaration) {
// TODO(anhans): A block should be context-sensitive if it has a context-sensitive return value.
return !node.typeParameters && !getEffectiveReturnTypeNode(node) && !!node.body && node.body.kind !== SyntaxKind.Block && isContextSensitive(node.body);
if (node.typeParameters || getEffectiveReturnTypeNode(node) || !node.body) {
return false;
}
if (node.body.kind !== SyntaxKind.Block) {
return isContextSensitive(node.body);
}
return !!forEachReturnStatement(node.body as Block, (statement) => !!statement.expression && isContextSensitive(statement.expression));
}

function isContextSensitiveFunctionOrObjectLiteralMethod(func: Node): func is FunctionExpression | ArrowFunction | MethodDeclaration {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/compiler/inferPropertyWithContextSensitiveReturnStatement.ts ===
// repro #50687

declare function repro<T>(config: {
>repro : Symbol(repro, Decl(inferPropertyWithContextSensitiveReturnStatement.ts, 0, 0))
>T : Symbol(T, Decl(inferPropertyWithContextSensitiveReturnStatement.ts, 2, 23))
>config : Symbol(config, Decl(inferPropertyWithContextSensitiveReturnStatement.ts, 2, 26))

params: T;
>params : Symbol(params, Decl(inferPropertyWithContextSensitiveReturnStatement.ts, 2, 35))
>T : Symbol(T, Decl(inferPropertyWithContextSensitiveReturnStatement.ts, 2, 23))

callback: () => (params: T) => number;
>callback : Symbol(callback, Decl(inferPropertyWithContextSensitiveReturnStatement.ts, 3, 12))
>params : Symbol(params, Decl(inferPropertyWithContextSensitiveReturnStatement.ts, 4, 19))
>T : Symbol(T, Decl(inferPropertyWithContextSensitiveReturnStatement.ts, 2, 23))

}): void;

repro({
>repro : Symbol(repro, Decl(inferPropertyWithContextSensitiveReturnStatement.ts, 0, 0))

params: 1,
>params : Symbol(params, Decl(inferPropertyWithContextSensitiveReturnStatement.ts, 7, 7))

callback: () => { return a => a + 1 },
>callback : Symbol(callback, Decl(inferPropertyWithContextSensitiveReturnStatement.ts, 8, 12))
>a : Symbol(a, Decl(inferPropertyWithContextSensitiveReturnStatement.ts, 9, 26))
>a : Symbol(a, Decl(inferPropertyWithContextSensitiveReturnStatement.ts, 9, 26))

});

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
=== tests/cases/compiler/inferPropertyWithContextSensitiveReturnStatement.ts ===
// repro #50687

declare function repro<T>(config: {
>repro : <T>(config: { params: T; callback: () => (params: T) => number; }) => void
>config : { params: T; callback: () => (params: T) => number; }

params: T;
>params : T

callback: () => (params: T) => number;
>callback : () => (params: T) => number
>params : T

}): void;

repro({
>repro({ params: 1, callback: () => { return a => a + 1 },}) : void
>repro : <T>(config: { params: T; callback: () => (params: T) => number; }) => void
>{ params: 1, callback: () => { return a => a + 1 },} : { params: number; callback: () => (a: number) => number; }

params: 1,
>params : number
>1 : 1

callback: () => { return a => a + 1 },
>callback : () => (a: number) => number
>() => { return a => a + 1 } : () => (a: number) => number
>a => a + 1 : (a: number) => number
>a : number
>a + 1 : number
>a : number
>1 : 1

});

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @noEmit: true
// @strict: true


// repro #50687

declare function repro<T>(config: {
params: T;
callback: () => (params: T) => number;
}): void;

repro({
params: 1,
callback: () => { return a => a + 1 },
});