Skip to content

Commit 702bc52

Browse files
authored
Add additional tests for intra expression inference (#48584)
1 parent 4c6f25a commit 702bc52

File tree

5 files changed

+393
-165
lines changed

5 files changed

+393
-165
lines changed

tests/baselines/reference/intraExpressionInferences.errors.txt

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferences.ts(123,5): error TS2322: Type '(inputs: Unwrap<{ num: Wrapper<number>; str: Wrapper<string>; }>) => { bool: any; str: number; }' is not assignable to type '(inputs: Unwrap<{ num: Wrapper<number>; str: Wrapper<string>; }>) => Unwrap<{ bool: Wrapper<boolean>; str: Wrapper<string>; }>'.
1+
tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferences.ts(131,5): error TS2322: Type '(inputs: Unwrap<{ num: Wrapper<number>; str: Wrapper<string>; }>) => { bool: any; str: number; }' is not assignable to type '(inputs: Unwrap<{ num: Wrapper<number>; str: Wrapper<string>; }>) => Unwrap<{ bool: Wrapper<boolean>; str: Wrapper<string>; }>'.
22
Call signature return types '{ bool: any; str: number; }' and 'Unwrap<{ bool: Wrapper<boolean>; str: Wrapper<string>; }>' are incompatible.
33
The types of 'str' are incompatible between these types.
44
Type 'number' is not assignable to type 'string'.
5-
tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferences.ts(125,26): error TS2339: Property 'nonexistent' does not exist on type 'Unwrap<{ num: Wrapper<number>; str: Wrapper<string>; }>'.
5+
tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferences.ts(133,26): error TS2339: Property 'nonexistent' does not exist on type 'Unwrap<{ num: Wrapper<number>; str: Wrapper<string>; }>'.
66

77

88
==== tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferences.ts (2 errors) ====
@@ -97,6 +97,14 @@ tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInf
9797
}
9898
});
9999

100+
test({
101+
a: () => 0,
102+
b: (a) => a,
103+
c: (b) => {
104+
const x: number = b;
105+
}
106+
});
107+
100108
// Repro from #41712
101109

102110
class Wrapper<T = any> {
@@ -134,7 +142,7 @@ tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInf
134142
!!! error TS2322: Call signature return types '{ bool: any; str: number; }' and 'Unwrap<{ bool: Wrapper<boolean>; str: Wrapper<string>; }>' are incompatible.
135143
!!! error TS2322: The types of 'str' are incompatible between these types.
136144
!!! error TS2322: Type 'number' is not assignable to type 'string'.
137-
!!! related TS6500 tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferences.ts:105:5: The expected type comes from property 'map' which is declared here on type 'MappingComponent<{ num: Wrapper<number>; str: Wrapper<string>; }, { bool: Wrapper<boolean>; str: Wrapper<string>; }>'
145+
!!! related TS6500 tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInferences.ts:113:5: The expected type comes from property 'map' which is declared here on type 'MappingComponent<{ num: Wrapper<number>; str: Wrapper<string>; }, { bool: Wrapper<boolean>; str: Wrapper<string>; }>'
138146
return {
139147
bool: inputs.nonexistent,
140148
~~~~~~~~~~~
@@ -189,4 +197,19 @@ tests/cases/conformance/types/typeRelationships/typeInference/intraExpressionInf
189197
fetch: (params: Params, foo) => 123,
190198
map: (number) => String(number)
191199
});
200+
201+
// Repro from #45255
202+
203+
declare const branch:
204+
<T, U extends T>(_: { test: T, if: (t: T) => t is U, then: (u: U) => void }) => void
205+
206+
declare const x: "a" | "b"
207+
208+
branch({
209+
test: x,
210+
if: (t): t is "a" => t === "a",
211+
then: u => {
212+
let test1: "a" = u
213+
}
214+
})
192215

tests/baselines/reference/intraExpressionInferences.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ test({
9090
}
9191
});
9292

93+
test({
94+
a: () => 0,
95+
b: (a) => a,
96+
c: (b) => {
97+
const x: number = b;
98+
}
99+
});
100+
93101
// Repro from #41712
94102

95103
class Wrapper<T = any> {
@@ -174,6 +182,21 @@ example({
174182
fetch: (params: Params, foo) => 123,
175183
map: (number) => String(number)
176184
});
185+
186+
// Repro from #45255
187+
188+
declare const branch:
189+
<T, U extends T>(_: { test: T, if: (t: T) => t is U, then: (u: U) => void }) => void
190+
191+
declare const x: "a" | "b"
192+
193+
branch({
194+
test: x,
195+
if: (t): t is "a" => t === "a",
196+
then: u => {
197+
let test1: "a" = u
198+
}
199+
})
177200

178201

179202
//// [intraExpressionInferences.js]
@@ -228,6 +251,13 @@ test({
228251
var x = b;
229252
}
230253
});
254+
test({
255+
a: function () { return 0; },
256+
b: function (a) { return a; },
257+
c: function (b) {
258+
var x = b;
259+
}
260+
});
231261
// Repro from #41712
232262
var Wrapper = /** @class */ (function () {
233263
function Wrapper() {
@@ -279,6 +309,13 @@ example({
279309
fetch: function (params, foo) { return 123; },
280310
map: function (number) { return String(number); }
281311
});
312+
branch({
313+
test: x,
314+
"if": function (t) { return t === "a"; },
315+
then: function (u) {
316+
var test1 = u;
317+
}
318+
});
282319

283320

284321
//// [intraExpressionInferences.d.ts]
@@ -340,3 +377,9 @@ interface Params {
340377
one: number;
341378
two: string;
342379
}
380+
declare const branch: <T, U extends T>(_: {
381+
test: T;
382+
if: (t: T) => t is U;
383+
then: (u: U) => void;
384+
}) => void;
385+
declare const x: "a" | "b";

0 commit comments

Comments
 (0)