Skip to content

Commit 21b8892

Browse files
authored
Fixed an issue with errors not being correctly reported after completion requests in functions within nested calls (#54944)
1 parent 05fdb5f commit 21b8892

File tree

3 files changed

+189
-4
lines changed

3 files changed

+189
-4
lines changed

src/compiler/checker.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,20 +1836,30 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
18361836
};
18371837

18381838
function runWithoutResolvedSignatureCaching<T>(node: Node | undefined, fn: () => T): T {
1839-
const cachedSignatures = [];
1839+
const cachedResolvedSignatures = [];
1840+
const cachedTypes = [];
18401841
while (node) {
1841-
if (isCallLikeExpression(node)) {
1842+
if (isCallLikeExpression(node) || isFunctionLike(node)) {
18421843
const nodeLinks = getNodeLinks(node);
18431844
const resolvedSignature = nodeLinks.resolvedSignature;
1844-
cachedSignatures.push([nodeLinks, resolvedSignature] as const);
1845+
cachedResolvedSignatures.push([nodeLinks, resolvedSignature] as const);
18451846
nodeLinks.resolvedSignature = undefined;
18461847
}
1848+
if (isFunctionLike(node)) {
1849+
const symbolLinks = getSymbolLinks(getSymbolOfDeclaration(node));
1850+
const type = symbolLinks.type;
1851+
cachedTypes.push([symbolLinks, type] as const);
1852+
symbolLinks.type = undefined;
1853+
}
18471854
node = node.parent;
18481855
}
18491856
const result = fn();
1850-
for (const [nodeLinks, resolvedSignature] of cachedSignatures) {
1857+
for (const [nodeLinks, resolvedSignature] of cachedResolvedSignatures) {
18511858
nodeLinks.resolvedSignature = resolvedSignature;
18521859
}
1860+
for (const [symbolLinks, type] of cachedTypes) {
1861+
symbolLinks.type = type;
1862+
}
18531863
return result;
18541864
}
18551865

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
Syntactic Diagnostics for file '/tests/cases/fourslash/typeErrorAfterStringCompletionsInNestedCall2.ts':
2+
3+
4+
==== /tests/cases/fourslash/typeErrorAfterStringCompletionsInNestedCall2.ts (0 errors) ====
5+
6+
type ActionFunction<
7+
TExpressionEvent extends { type: string },
8+
out TEvent extends { type: string }
9+
> = {
10+
({ event }: { event: TExpressionEvent }): void;
11+
_out_TEvent?: TEvent;
12+
};
13+
14+
interface MachineConfig<TEvent extends { type: string }> {
15+
types: {
16+
events: TEvent;
17+
};
18+
on: {
19+
[K in TEvent["type"]]?: ActionFunction<
20+
Extract<TEvent, { type: K }>,
21+
TEvent
22+
>;
23+
};
24+
}
25+
26+
declare function raise<
27+
TExpressionEvent extends { type: string },
28+
TEvent extends { type: string }
29+
>(
30+
resolve: ({ event }: { event: TExpressionEvent }) => TEvent
31+
): {
32+
({ event }: { event: TExpressionEvent }): void;
33+
_out_TEvent?: TEvent;
34+
};
35+
36+
declare function createMachine<TEvent extends { type: string }>(
37+
config: MachineConfig<TEvent>
38+
): void;
39+
40+
createMachine({
41+
types: {
42+
events: {} as { type: "FOO" } | { type: "BAR" },
43+
},
44+
on: {
45+
FOO: raise(({ event }) => {
46+
return {
47+
type: "BAR" as const,
48+
};
49+
}),
50+
},
51+
});
52+
53+
Semantic Diagnostics for file '/tests/cases/fourslash/typeErrorAfterStringCompletionsInNestedCall2.ts':
54+
/tests/cases/fourslash/typeErrorAfterStringCompletionsInNestedCall2.ts(41,5): error TS2322: Type '{ ({ event }: { event: { type: "FOO"; }; }): void; _out_TEvent?: { type: "BARx"; } | undefined; }' is not assignable to type 'ActionFunction<{ type: "FOO"; }, { type: "FOO"; } | { type: "BAR"; }>'.
55+
Types of property '_out_TEvent' are incompatible.
56+
Type '{ type: "BARx"; } | undefined' is not assignable to type '{ type: "FOO"; } | { type: "BAR"; } | undefined'.
57+
Type '{ type: "BARx"; }' is not assignable to type '{ type: "FOO"; } | { type: "BAR"; } | undefined'.
58+
Type '{ type: "BARx"; }' is not assignable to type '{ type: "FOO"; } | { type: "BAR"; }'.
59+
Type '{ type: "BARx"; }' is not assignable to type '{ type: "BAR"; }'.
60+
Types of property 'type' are incompatible.
61+
Type '"BARx"' is not assignable to type '"BAR"'.
62+
63+
64+
==== /tests/cases/fourslash/typeErrorAfterStringCompletionsInNestedCall2.ts (1 errors) ====
65+
66+
type ActionFunction<
67+
TExpressionEvent extends { type: string },
68+
out TEvent extends { type: string }
69+
> = {
70+
({ event }: { event: TExpressionEvent }): void;
71+
_out_TEvent?: TEvent;
72+
};
73+
74+
interface MachineConfig<TEvent extends { type: string }> {
75+
types: {
76+
events: TEvent;
77+
};
78+
on: {
79+
[K in TEvent["type"]]?: ActionFunction<
80+
Extract<TEvent, { type: K }>,
81+
TEvent
82+
>;
83+
};
84+
}
85+
86+
declare function raise<
87+
TExpressionEvent extends { type: string },
88+
TEvent extends { type: string }
89+
>(
90+
resolve: ({ event }: { event: TExpressionEvent }) => TEvent
91+
): {
92+
({ event }: { event: TExpressionEvent }): void;
93+
_out_TEvent?: TEvent;
94+
};
95+
96+
declare function createMachine<TEvent extends { type: string }>(
97+
config: MachineConfig<TEvent>
98+
): void;
99+
100+
createMachine({
101+
types: {
102+
events: {} as { type: "FOO" } | { type: "BAR" },
103+
},
104+
on: {
105+
FOO: raise(({ event }) => {
106+
~~~
107+
!!! error TS2322: Type '{ ({ event }: { event: { type: "FOO"; }; }): void; _out_TEvent?: { type: "BARx"; } | undefined; }' is not assignable to type 'ActionFunction<{ type: "FOO"; }, { type: "FOO"; } | { type: "BAR"; }>'.
108+
!!! error TS2322: Types of property '_out_TEvent' are incompatible.
109+
!!! error TS2322: Type '{ type: "BARx"; } | undefined' is not assignable to type '{ type: "FOO"; } | { type: "BAR"; } | undefined'.
110+
!!! error TS2322: Type '{ type: "BARx"; }' is not assignable to type '{ type: "FOO"; } | { type: "BAR"; } | undefined'.
111+
!!! error TS2322: Type '{ type: "BARx"; }' is not assignable to type '{ type: "FOO"; } | { type: "BAR"; }'.
112+
!!! error TS2322: Type '{ type: "BARx"; }' is not assignable to type '{ type: "BAR"; }'.
113+
!!! error TS2322: Types of property 'type' are incompatible.
114+
!!! error TS2322: Type '"BARx"' is not assignable to type '"BAR"'.
115+
!!! related TS6500 /tests/cases/fourslash/typeErrorAfterStringCompletionsInNestedCall2.ts:14:7: The expected type comes from property 'FOO' which is declared here on type '{ FOO?: ActionFunction<{ type: "FOO"; }, { type: "FOO"; } | { type: "BAR"; }> | undefined; BAR?: ActionFunction<{ type: "BAR"; }, { type: "FOO"; } | { type: "BAR"; }> | undefined; }'
116+
return {
117+
type: "BAR" as const,
118+
};
119+
}),
120+
},
121+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
///<reference path="fourslash.ts"/>
2+
// @strict: true
3+
////
4+
//// type ActionFunction<
5+
//// TExpressionEvent extends { type: string },
6+
//// out TEvent extends { type: string }
7+
//// > = {
8+
//// ({ event }: { event: TExpressionEvent }): void;
9+
//// _out_TEvent?: TEvent;
10+
//// };
11+
////
12+
//// interface MachineConfig<TEvent extends { type: string }> {
13+
//// types: {
14+
//// events: TEvent;
15+
//// };
16+
//// on: {
17+
//// [K in TEvent["type"]]?: ActionFunction<
18+
//// Extract<TEvent, { type: K }>,
19+
//// TEvent
20+
//// >;
21+
//// };
22+
//// }
23+
////
24+
//// declare function raise<
25+
//// TExpressionEvent extends { type: string },
26+
//// TEvent extends { type: string }
27+
//// >(
28+
//// resolve: ({ event }: { event: TExpressionEvent }) => TEvent
29+
//// ): {
30+
//// ({ event }: { event: TExpressionEvent }): void;
31+
//// _out_TEvent?: TEvent;
32+
//// };
33+
////
34+
//// declare function createMachine<TEvent extends { type: string }>(
35+
//// config: MachineConfig<TEvent>
36+
//// ): void;
37+
////
38+
//// createMachine({
39+
//// types: {
40+
//// events: {} as { type: "FOO" } | { type: "BAR" },
41+
//// },
42+
//// on: {
43+
//// [|/*error*/FOO|]: raise(({ event }) => {
44+
//// return {
45+
//// type: "BAR/*1*/" as const,
46+
//// };
47+
//// }),
48+
//// },
49+
//// });
50+
51+
goTo.marker("1");
52+
edit.insert(`x`)
53+
verify.completions({ exact: ["FOO", "BAR"] });
54+
verify.baselineSyntacticAndSemanticDiagnostics()

0 commit comments

Comments
 (0)