From 23746af766b53fcc3afecfa16478809a5a36628a Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Tue, 20 Sep 2022 21:03:18 +0300 Subject: [PATCH] fix(50591): RangeError: Maximum call stack size exceeded (#50594) --- src/compiler/checker.ts | 2 +- ...sIndexedAccessOfKnownProperty10.errors.txt | 15 +++++++++++ ...rdNarrowsIndexedAccessOfKnownProperty10.js | 17 ++++++++++++ ...rowsIndexedAccessOfKnownProperty10.symbols | 24 +++++++++++++++++ ...arrowsIndexedAccessOfKnownProperty10.types | 26 +++++++++++++++++++ ...rdNarrowsIndexedAccessOfKnownProperty10.ts | 9 +++++++ 6 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/typeGuardNarrowsIndexedAccessOfKnownProperty10.errors.txt create mode 100644 tests/baselines/reference/typeGuardNarrowsIndexedAccessOfKnownProperty10.js create mode 100644 tests/baselines/reference/typeGuardNarrowsIndexedAccessOfKnownProperty10.symbols create mode 100644 tests/baselines/reference/typeGuardNarrowsIndexedAccessOfKnownProperty10.types create mode 100644 tests/cases/compiler/typeGuardNarrowsIndexedAccessOfKnownProperty10.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8f252f80f8c53..96cd22e9c7aa8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -23660,7 +23660,7 @@ namespace ts { } } - if (hasOnlyExpressionInitializer(declaration)) { + if (hasOnlyExpressionInitializer(declaration) && isBlockScopedNameDeclaredBeforeUse(declaration, node.argumentExpression)) { const initializer = getEffectiveInitializer(declaration); return initializer && tryGetNameFromType(getTypeOfExpression(initializer)); } diff --git a/tests/baselines/reference/typeGuardNarrowsIndexedAccessOfKnownProperty10.errors.txt b/tests/baselines/reference/typeGuardNarrowsIndexedAccessOfKnownProperty10.errors.txt new file mode 100644 index 0000000000000..5e47b3fa45159 --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsIndexedAccessOfKnownProperty10.errors.txt @@ -0,0 +1,15 @@ +tests/cases/compiler/typeGuardNarrowsIndexedAccessOfKnownProperty10.ts(5,6): error TS2448: Block-scoped variable 'id' used before its declaration. + + +==== tests/cases/compiler/typeGuardNarrowsIndexedAccessOfKnownProperty10.ts (1 errors) ==== + interface Foo { bar: any; } + const bar: { [id: string]: number } = {}; + + (foo: Foo) => { + bar[id]++; + ~~ +!!! error TS2448: Block-scoped variable 'id' used before its declaration. +!!! related TS2728 tests/cases/compiler/typeGuardNarrowsIndexedAccessOfKnownProperty10.ts:6:8: 'id' is declared here. + const id = foo.bar; + } + \ No newline at end of file diff --git a/tests/baselines/reference/typeGuardNarrowsIndexedAccessOfKnownProperty10.js b/tests/baselines/reference/typeGuardNarrowsIndexedAccessOfKnownProperty10.js new file mode 100644 index 0000000000000..49dfc42d31d25 --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsIndexedAccessOfKnownProperty10.js @@ -0,0 +1,17 @@ +//// [typeGuardNarrowsIndexedAccessOfKnownProperty10.ts] +interface Foo { bar: any; } +const bar: { [id: string]: number } = {}; + +(foo: Foo) => { + bar[id]++; + const id = foo.bar; +} + + +//// [typeGuardNarrowsIndexedAccessOfKnownProperty10.js] +"use strict"; +var bar = {}; +(function (foo) { + bar[id]++; + var id = foo.bar; +}); diff --git a/tests/baselines/reference/typeGuardNarrowsIndexedAccessOfKnownProperty10.symbols b/tests/baselines/reference/typeGuardNarrowsIndexedAccessOfKnownProperty10.symbols new file mode 100644 index 0000000000000..664f88d20f12c --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsIndexedAccessOfKnownProperty10.symbols @@ -0,0 +1,24 @@ +=== tests/cases/compiler/typeGuardNarrowsIndexedAccessOfKnownProperty10.ts === +interface Foo { bar: any; } +>Foo : Symbol(Foo, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty10.ts, 0, 0)) +>bar : Symbol(Foo.bar, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty10.ts, 0, 15)) + +const bar: { [id: string]: number } = {}; +>bar : Symbol(bar, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty10.ts, 1, 5)) +>id : Symbol(id, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty10.ts, 1, 14)) + +(foo: Foo) => { +>foo : Symbol(foo, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty10.ts, 3, 1)) +>Foo : Symbol(Foo, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty10.ts, 0, 0)) + + bar[id]++; +>bar : Symbol(bar, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty10.ts, 1, 5)) +>id : Symbol(id, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty10.ts, 5, 6)) + + const id = foo.bar; +>id : Symbol(id, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty10.ts, 5, 6)) +>foo.bar : Symbol(Foo.bar, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty10.ts, 0, 15)) +>foo : Symbol(foo, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty10.ts, 3, 1)) +>bar : Symbol(Foo.bar, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty10.ts, 0, 15)) +} + diff --git a/tests/baselines/reference/typeGuardNarrowsIndexedAccessOfKnownProperty10.types b/tests/baselines/reference/typeGuardNarrowsIndexedAccessOfKnownProperty10.types new file mode 100644 index 0000000000000..14138aa4f0729 --- /dev/null +++ b/tests/baselines/reference/typeGuardNarrowsIndexedAccessOfKnownProperty10.types @@ -0,0 +1,26 @@ +=== tests/cases/compiler/typeGuardNarrowsIndexedAccessOfKnownProperty10.ts === +interface Foo { bar: any; } +>bar : any + +const bar: { [id: string]: number } = {}; +>bar : { [id: string]: number; } +>id : string +>{} : {} + +(foo: Foo) => { +>(foo: Foo) => { bar[id]++; const id = foo.bar;} : (foo: Foo) => void +>foo : Foo + + bar[id]++; +>bar[id]++ : number +>bar[id] : number +>bar : { [id: string]: number; } +>id : any + + const id = foo.bar; +>id : any +>foo.bar : any +>foo : Foo +>bar : any +} + diff --git a/tests/cases/compiler/typeGuardNarrowsIndexedAccessOfKnownProperty10.ts b/tests/cases/compiler/typeGuardNarrowsIndexedAccessOfKnownProperty10.ts new file mode 100644 index 0000000000000..7ae9324f6ce85 --- /dev/null +++ b/tests/cases/compiler/typeGuardNarrowsIndexedAccessOfKnownProperty10.ts @@ -0,0 +1,9 @@ +// @strict: true + +interface Foo { bar: any; } +const bar: { [id: string]: number } = {}; + +(foo: Foo) => { + bar[id]++; + const id = foo.bar; +}