Skip to content

Commit 760393f

Browse files
ajafffRyanCavanaugh
authored andcommitted
fix false positive 'variable is used before being assigned' in destructuring (microsoft#29636)
Fixes: microsoft#29458
1 parent 6487d1f commit 760393f

6 files changed

+78
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17089,7 +17089,7 @@ namespace ts {
1708917089
// We only look for uninitialized variables in strict null checking mode, and only when we can analyze
1709017090
// the entire control flow graph from the variable's declaration (i.e. when the flow container and
1709117091
// declaration container are the same).
17092-
const assumeInitialized = isParameter || isAlias || isOuterVariable || isSpreadDestructuringAssignmentTarget || isModuleExports ||
17092+
const assumeInitialized = isParameter || isAlias || isOuterVariable || isSpreadDestructuringAssignmentTarget || isModuleExports || isBindingElement(declaration) ||
1709317093
type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & TypeFlags.AnyOrUnknown) !== 0 ||
1709417094
isInTypeQuery(node) || node.parent.kind === SyntaxKind.ExportSpecifier) ||
1709517095
node.parent.kind === SyntaxKind.NonNullExpression ||
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
tests/cases/compiler/useBeforeDeclaration_destructuring.ts(1,1): error TS2448: Block-scoped variable 'a' used before its declaration.
2+
3+
4+
==== tests/cases/compiler/useBeforeDeclaration_destructuring.ts (1 errors) ====
5+
a;
6+
~
7+
!!! error TS2448: Block-scoped variable 'a' used before its declaration.
8+
!!! related TS2728 tests/cases/compiler/useBeforeDeclaration_destructuring.ts:2:6: 'a' is declared here.
9+
let {a, b = a} = {a: '', b: 1};
10+
b;
11+
12+
function test({c, d = c}: Record<string, number>) {}
13+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [useBeforeDeclaration_destructuring.ts]
2+
a;
3+
let {a, b = a} = {a: '', b: 1};
4+
b;
5+
6+
function test({c, d = c}: Record<string, number>) {}
7+
8+
9+
//// [useBeforeDeclaration_destructuring.js]
10+
a;
11+
var _a = { a: '', b: 1 }, a = _a.a, _b = _a.b, b = _b === void 0 ? a : _b;
12+
b;
13+
function test(_a) {
14+
var c = _a.c, _b = _a.d, d = _b === void 0 ? c : _b;
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/useBeforeDeclaration_destructuring.ts ===
2+
a;
3+
>a : Symbol(a, Decl(useBeforeDeclaration_destructuring.ts, 1, 5))
4+
5+
let {a, b = a} = {a: '', b: 1};
6+
>a : Symbol(a, Decl(useBeforeDeclaration_destructuring.ts, 1, 5))
7+
>b : Symbol(b, Decl(useBeforeDeclaration_destructuring.ts, 1, 7))
8+
>a : Symbol(a, Decl(useBeforeDeclaration_destructuring.ts, 1, 5))
9+
>a : Symbol(a, Decl(useBeforeDeclaration_destructuring.ts, 1, 18))
10+
>b : Symbol(b, Decl(useBeforeDeclaration_destructuring.ts, 1, 24))
11+
12+
b;
13+
>b : Symbol(b, Decl(useBeforeDeclaration_destructuring.ts, 1, 7))
14+
15+
function test({c, d = c}: Record<string, number>) {}
16+
>test : Symbol(test, Decl(useBeforeDeclaration_destructuring.ts, 2, 2))
17+
>c : Symbol(c, Decl(useBeforeDeclaration_destructuring.ts, 4, 15))
18+
>d : Symbol(d, Decl(useBeforeDeclaration_destructuring.ts, 4, 17))
19+
>c : Symbol(c, Decl(useBeforeDeclaration_destructuring.ts, 4, 15))
20+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
21+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/compiler/useBeforeDeclaration_destructuring.ts ===
2+
a;
3+
>a : any
4+
5+
let {a, b = a} = {a: '', b: 1};
6+
>a : any
7+
>b : any
8+
>a : any
9+
>{a: '', b: 1} : { a: string; b?: number; }
10+
>a : string
11+
>'' : ""
12+
>b : number
13+
>1 : 1
14+
15+
b;
16+
>b : any
17+
18+
function test({c, d = c}: Record<string, number>) {}
19+
>test : ({ c, d }: Record<string, number>) => void
20+
>c : number
21+
>d : number
22+
>c : number
23+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
a;
2+
let {a, b = a} = {a: '', b: 1};
3+
b;
4+
5+
function test({c, d = c}: Record<string, number>) {}

0 commit comments

Comments
 (0)