Skip to content

Commit 2ea2ecf

Browse files
authored
Fixed crash in hasVisibleDeclarations related to binding elements (#61352)
1 parent c33f83a commit 2ea2ecf

5 files changed

+187
-1
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5981,7 +5981,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
59815981
return addVisibleAlias(declaration, declaration.parent.parent.parent.parent);
59825982
}
59835983
else if (symbol.flags & SymbolFlags.BlockScopedVariable) {
5984-
const variableStatement = findAncestor(declaration, isVariableStatement)!;
5984+
const rootDeclaration = walkUpBindingElementsAndPatterns(declaration);
5985+
if (rootDeclaration.kind === SyntaxKind.Parameter) {
5986+
return false;
5987+
}
5988+
const variableStatement = rootDeclaration.parent.parent;
5989+
if (variableStatement.kind !== SyntaxKind.VariableStatement) {
5990+
return false;
5991+
}
59855992
if (hasSyntacticModifier(variableStatement, ModifierFlags.Export)) {
59865993
return true;
59875994
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
computedPropertyBindingElementDeclarationNoCrash1.ts(12,21): error TS2345: Argument of type '{ [x: string]: unknown; }' is not assignable to parameter of type 'State'.
2+
Type '{ [x: string]: unknown; }' is missing the following properties from type 'State': a, b
3+
4+
5+
==== computedPropertyBindingElementDeclarationNoCrash1.ts (1 errors) ====
6+
// https://github.com/microsoft/TypeScript/issues/61351
7+
8+
export type State = {
9+
a: number;
10+
b: string;
11+
};
12+
13+
export class Test {
14+
setState(state: State) {}
15+
test = (e: any) => {
16+
for (const [key, value] of Object.entries(e)) {
17+
this.setState({
18+
~
19+
[key]: value,
20+
~~~~~~~~~~~~~~~~~~~~~
21+
});
22+
~~~~~~~
23+
!!! error TS2345: Argument of type '{ [x: string]: unknown; }' is not assignable to parameter of type 'State'.
24+
!!! error TS2345: Type '{ [x: string]: unknown; }' is missing the following properties from type 'State': a, b
25+
}
26+
};
27+
}
28+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//// [tests/cases/compiler/computedPropertyBindingElementDeclarationNoCrash1.ts] ////
2+
3+
=== computedPropertyBindingElementDeclarationNoCrash1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/61351
5+
6+
export type State = {
7+
>State : Symbol(State, Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 0, 0))
8+
9+
a: number;
10+
>a : Symbol(a, Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 2, 21))
11+
12+
b: string;
13+
>b : Symbol(b, Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 3, 12))
14+
15+
};
16+
17+
export class Test {
18+
>Test : Symbol(Test, Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 5, 2))
19+
20+
setState(state: State) {}
21+
>setState : Symbol(Test.setState, Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 7, 19))
22+
>state : Symbol(state, Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 8, 11))
23+
>State : Symbol(State, Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 0, 0))
24+
25+
test = (e: any) => {
26+
>test : Symbol(Test.test, Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 8, 27))
27+
>e : Symbol(e, Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 9, 10))
28+
29+
for (const [key, value] of Object.entries(e)) {
30+
>key : Symbol(key, Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 10, 16))
31+
>value : Symbol(value, Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 10, 20))
32+
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
33+
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
34+
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
35+
>e : Symbol(e, Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 9, 10))
36+
37+
this.setState({
38+
>this.setState : Symbol(Test.setState, Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 7, 19))
39+
>this : Symbol(Test, Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 5, 2))
40+
>setState : Symbol(Test.setState, Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 7, 19))
41+
42+
[key]: value,
43+
>[key] : Symbol([key], Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 11, 21))
44+
>key : Symbol(key, Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 10, 16))
45+
>value : Symbol(value, Decl(computedPropertyBindingElementDeclarationNoCrash1.ts, 10, 20))
46+
47+
});
48+
}
49+
};
50+
}
51+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//// [tests/cases/compiler/computedPropertyBindingElementDeclarationNoCrash1.ts] ////
2+
3+
=== computedPropertyBindingElementDeclarationNoCrash1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/61351
5+
6+
export type State = {
7+
>State : State
8+
> : ^^^^^
9+
10+
a: number;
11+
>a : number
12+
> : ^^^^^^
13+
14+
b: string;
15+
>b : string
16+
> : ^^^^^^
17+
18+
};
19+
20+
export class Test {
21+
>Test : Test
22+
> : ^^^^
23+
24+
setState(state: State) {}
25+
>setState : (state: State) => void
26+
> : ^ ^^ ^^^^^^^^^
27+
>state : State
28+
> : ^^^^^
29+
30+
test = (e: any) => {
31+
>test : (e: any) => void
32+
> : ^ ^^ ^^^^^^^^^
33+
>(e: any) => { for (const [key, value] of Object.entries(e)) { this.setState({ [key]: value, }); } } : (e: any) => void
34+
> : ^ ^^ ^^^^^^^^^
35+
>e : any
36+
> : ^^^
37+
38+
for (const [key, value] of Object.entries(e)) {
39+
>key : string
40+
> : ^^^^^^
41+
>value : unknown
42+
> : ^^^^^^^
43+
>Object.entries(e) : [string, unknown][]
44+
> : ^^^^^^^^^^^^^^^^^^^
45+
>Object.entries : { <T>(o: { [s: string]: T; } | ArrayLike<T>): [string, T][]; (o: {}): [string, any][]; }
46+
> : ^^^ ^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^
47+
>Object : ObjectConstructor
48+
> : ^^^^^^^^^^^^^^^^^
49+
>entries : { <T>(o: { [s: string]: T; } | ArrayLike<T>): [string, T][]; (o: {}): [string, any][]; }
50+
> : ^^^ ^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^
51+
>e : any
52+
> : ^^^
53+
54+
this.setState({
55+
>this.setState({ [key]: value, }) : void
56+
> : ^^^^
57+
>this.setState : (state: State) => void
58+
> : ^ ^^ ^^^^^^^^^
59+
>this : this
60+
> : ^^^^
61+
>setState : (state: State) => void
62+
> : ^ ^^ ^^^^^^^^^
63+
>{ [key]: value, } : { [x: string]: unknown; }
64+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^
65+
66+
[key]: value,
67+
>[key] : unknown
68+
> : ^^^^^^^
69+
>key : string
70+
> : ^^^^^^
71+
>value : unknown
72+
> : ^^^^^^^
73+
74+
});
75+
}
76+
};
77+
}
78+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @strict: true
2+
// @target: esnext
3+
// @lib: esnext
4+
// @noEmit: true
5+
6+
// https://github.com/microsoft/TypeScript/issues/61351
7+
8+
export type State = {
9+
a: number;
10+
b: string;
11+
};
12+
13+
export class Test {
14+
setState(state: State) {}
15+
test = (e: any) => {
16+
for (const [key, value] of Object.entries(e)) {
17+
this.setState({
18+
[key]: value,
19+
});
20+
}
21+
};
22+
}

0 commit comments

Comments
 (0)