Skip to content

Commit 728064d

Browse files
committed
Don't try to narrow type of a symbol within RHS of its declaration
1 parent a586c34 commit 728064d

File tree

5 files changed

+466
-1
lines changed

5 files changed

+466
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30838,7 +30838,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3083830838
function getNarrowedTypeOfSymbol(symbol: Symbol, location: Identifier) {
3083930839
const type = getTypeOfSymbol(symbol);
3084030840
const declaration = symbol.valueDeclaration;
30841-
if (declaration) {
30841+
if (declaration && !textRangeContainsPositionInclusive(getRootDeclaration(declaration), location.pos)) {
3084230842
// If we have a non-rest binding element with no initializer declared as a const variable or a const-like
3084330843
// parameter (a parameter for which there are no assignments in the function body), and if the parent type
3084430844
// for the destructuring is a union type, one or more of the binding elements may represent discriminant
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
dependentDestructuredVariablesRefereinceInDeclaration1.ts(4,9): error TS2322: Type '{ c: number; f: any; }' is not assignable to type 'string | number'.
2+
dependentDestructuredVariablesRefereinceInDeclaration1.ts(4,11): error TS2339: Property 'c' does not exist on type 'string | number'.
3+
dependentDestructuredVariablesRefereinceInDeclaration1.ts(4,14): error TS2339: Property 'f' does not exist on type 'string | number'.
4+
dependentDestructuredVariablesRefereinceInDeclaration1.ts(4,14): error TS7022: 'f' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
5+
dependentDestructuredVariablesRefereinceInDeclaration1.ts(4,45): error TS2448: Block-scoped variable 'f' used before its declaration.
6+
dependentDestructuredVariablesRefereinceInDeclaration1.ts(9,9): error TS2322: Type '{ c: number; f: () => any; }' is not assignable to type 'string | number'.
7+
dependentDestructuredVariablesRefereinceInDeclaration1.ts(9,11): error TS2339: Property 'c' does not exist on type 'string | number'.
8+
dependentDestructuredVariablesRefereinceInDeclaration1.ts(9,14): error TS2339: Property 'f' does not exist on type 'string | number'.
9+
dependentDestructuredVariablesRefereinceInDeclaration1.ts(14,9): error TS2322: Type '{ c: number; f: number; g: number; }' is not assignable to type 'string | number'.
10+
dependentDestructuredVariablesRefereinceInDeclaration1.ts(14,11): error TS2339: Property 'c' does not exist on type 'string | number'.
11+
dependentDestructuredVariablesRefereinceInDeclaration1.ts(14,14): error TS2339: Property 'f' does not exist on type 'string | number'.
12+
dependentDestructuredVariablesRefereinceInDeclaration1.ts(14,17): error TS2339: Property 'g' does not exist on type 'string | number'.
13+
dependentDestructuredVariablesRefereinceInDeclaration1.ts(20,14): error TS7022: 'f' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
14+
dependentDestructuredVariablesRefereinceInDeclaration1.ts(20,71): error TS2448: Block-scoped variable 'f' used before its declaration.
15+
16+
17+
==== dependentDestructuredVariablesRefereinceInDeclaration1.ts (14 errors) ====
18+
// https://github.com/microsoft/TypeScript/issues/62993
19+
20+
{
21+
const { c, f }: string | number = { c: 0, f };
22+
~~~~~~~~
23+
!!! error TS2322: Type '{ c: number; f: any; }' is not assignable to type 'string | number'.
24+
~
25+
!!! error TS2339: Property 'c' does not exist on type 'string | number'.
26+
~
27+
!!! error TS2339: Property 'f' does not exist on type 'string | number'.
28+
~
29+
!!! error TS7022: 'f' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
30+
~
31+
!!! error TS2448: Block-scoped variable 'f' used before its declaration.
32+
!!! related TS2728 dependentDestructuredVariablesRefereinceInDeclaration1.ts:4:14: 'f' is declared here.
33+
f;
34+
}
35+
36+
{
37+
const { c, f }: string | number = { c: 0, f: () => f };
38+
~~~~~~~~
39+
!!! error TS2322: Type '{ c: number; f: () => any; }' is not assignable to type 'string | number'.
40+
~
41+
!!! error TS2339: Property 'c' does not exist on type 'string | number'.
42+
~
43+
!!! error TS2339: Property 'f' does not exist on type 'string | number'.
44+
f;
45+
}
46+
47+
{
48+
const { c, f, g = f }: string | number = { c: 0, f: 0, g: 0 };
49+
~~~~~~~~~~~~~~~
50+
!!! error TS2322: Type '{ c: number; f: number; g: number; }' is not assignable to type 'string | number'.
51+
~
52+
!!! error TS2339: Property 'c' does not exist on type 'string | number'.
53+
~
54+
!!! error TS2339: Property 'f' does not exist on type 'string | number'.
55+
~
56+
!!! error TS2339: Property 'g' does not exist on type 'string | number'.
57+
f;
58+
g;
59+
}
60+
61+
{
62+
const { c, f }: { c: 0; f: number } | { c: 1; f: string } = { c: 0, f };
63+
~
64+
!!! error TS7022: 'f' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
65+
~
66+
!!! error TS2448: Block-scoped variable 'f' used before its declaration.
67+
!!! related TS2728 dependentDestructuredVariablesRefereinceInDeclaration1.ts:20:14: 'f' is declared here.
68+
f;
69+
}
70+
71+
{
72+
const { c, f }: { c: 0; f: () => unknown } | { c: 1; f: string } = {
73+
c: 0,
74+
f: () => f,
75+
};
76+
f;
77+
}
78+
79+
{
80+
const {
81+
c,
82+
f,
83+
g = f,
84+
}:
85+
| { c: 0; f: bigint; g?: bigint | number }
86+
| { c: 1; f: number; g: string } = {
87+
c: 0,
88+
f: 10n,
89+
};
90+
f;
91+
g;
92+
}
93+
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
//// [tests/cases/conformance/controlFlow/dependentDestructuredVariablesRefereinceInDeclaration1.ts] ////
2+
3+
=== dependentDestructuredVariablesRefereinceInDeclaration1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/62993
5+
6+
{
7+
const { c, f }: string | number = { c: 0, f };
8+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 3, 9))
9+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 3, 12))
10+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 3, 37))
11+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 3, 43))
12+
13+
f;
14+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 3, 12))
15+
}
16+
17+
{
18+
const { c, f }: string | number = { c: 0, f: () => f };
19+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 8, 9))
20+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 8, 12))
21+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 8, 37))
22+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 8, 43))
23+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 8, 12))
24+
25+
f;
26+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 8, 12))
27+
}
28+
29+
{
30+
const { c, f, g = f }: string | number = { c: 0, f: 0, g: 0 };
31+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 13, 9))
32+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 13, 12))
33+
>g : Symbol(g, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 13, 15))
34+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 13, 12))
35+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 13, 44))
36+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 13, 50))
37+
>g : Symbol(g, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 13, 56))
38+
39+
f;
40+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 13, 12))
41+
42+
g;
43+
>g : Symbol(g, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 13, 15))
44+
}
45+
46+
{
47+
const { c, f }: { c: 0; f: number } | { c: 1; f: string } = { c: 0, f };
48+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 19, 9))
49+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 19, 12))
50+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 19, 19))
51+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 19, 25))
52+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 19, 41))
53+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 19, 47))
54+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 19, 63))
55+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 19, 69))
56+
57+
f;
58+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 19, 12))
59+
}
60+
61+
{
62+
const { c, f }: { c: 0; f: () => unknown } | { c: 1; f: string } = {
63+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 24, 9))
64+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 24, 12))
65+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 24, 19))
66+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 24, 25))
67+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 24, 48))
68+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 24, 54))
69+
70+
c: 0,
71+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 24, 70))
72+
73+
f: () => f,
74+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 25, 9))
75+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 24, 12))
76+
77+
};
78+
f;
79+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 24, 12))
80+
}
81+
82+
{
83+
const {
84+
c,
85+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 32, 9))
86+
87+
f,
88+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 33, 6))
89+
90+
g = f,
91+
>g : Symbol(g, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 34, 6))
92+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 33, 6))
93+
94+
}:
95+
| { c: 0; f: bigint; g?: bigint | number }
96+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 37, 7))
97+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 37, 13))
98+
>g : Symbol(g, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 37, 24))
99+
100+
| { c: 1; f: number; g: string } = {
101+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 38, 7))
102+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 38, 13))
103+
>g : Symbol(g, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 38, 24))
104+
105+
c: 0,
106+
>c : Symbol(c, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 38, 40))
107+
108+
f: 10n,
109+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 39, 9))
110+
111+
};
112+
f;
113+
>f : Symbol(f, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 33, 6))
114+
115+
g;
116+
>g : Symbol(g, Decl(dependentDestructuredVariablesRefereinceInDeclaration1.ts, 34, 6))
117+
}
118+

0 commit comments

Comments
 (0)