diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f9fd563b87..5348d2b19f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -492,12 +492,16 @@ namespace ts { : false; } if (meaning & SymbolFlags.Value && result.flags & SymbolFlags.FunctionScopedVariable) { - // function scoped variables are visible only inside function body and parameter list - // technically here we might mix parameters and variables declared in function, - // however this case is detected separately when checking initializers of parameters + // parameters are visible only inside function body, parameter list and return type + // technically for parameter list case here we might mix parameters and variables declared in function, + // however it is detected separately when checking initializers of parameters // to make sure that they reference no variables declared after them. - useResult = lastLocation === (location).type || - lastLocation.kind === SyntaxKind.Parameter; + useResult = + lastLocation.kind === SyntaxKind.Parameter || + ( + lastLocation === (location).type && + result.valueDeclaration.kind === SyntaxKind.Parameter + ); } } diff --git a/tests/baselines/reference/functionVariableInReturnTypeAnnotation.errors.txt b/tests/baselines/reference/functionVariableInReturnTypeAnnotation.errors.txt new file mode 100644 index 0000000000..93a35292b7 --- /dev/null +++ b/tests/baselines/reference/functionVariableInReturnTypeAnnotation.errors.txt @@ -0,0 +1,10 @@ +tests/cases/compiler/functionVariableInReturnTypeAnnotation.ts(1,24): error TS2304: Cannot find name 'b'. + + +==== tests/cases/compiler/functionVariableInReturnTypeAnnotation.ts (1 errors) ==== + function bar(): typeof b { + ~ +!!! error TS2304: Cannot find name 'b'. + var b = 1; + return undefined; + } \ No newline at end of file diff --git a/tests/baselines/reference/functionVariableInReturnTypeAnnotation.js b/tests/baselines/reference/functionVariableInReturnTypeAnnotation.js new file mode 100644 index 0000000000..191255e52c --- /dev/null +++ b/tests/baselines/reference/functionVariableInReturnTypeAnnotation.js @@ -0,0 +1,11 @@ +//// [functionVariableInReturnTypeAnnotation.ts] +function bar(): typeof b { + var b = 1; + return undefined; +} + +//// [functionVariableInReturnTypeAnnotation.js] +function bar() { + var b = 1; + return undefined; +} diff --git a/tests/cases/compiler/functionVariableInReturnTypeAnnotation.ts b/tests/cases/compiler/functionVariableInReturnTypeAnnotation.ts new file mode 100644 index 0000000000..c8936034a2 --- /dev/null +++ b/tests/cases/compiler/functionVariableInReturnTypeAnnotation.ts @@ -0,0 +1,4 @@ +function bar(): typeof b { + var b = 1; + return undefined; +} \ No newline at end of file