Skip to content

Commit da6e82f

Browse files
committed
Use union types in the return type of functions in the error case
Fixes #6663
1 parent fd879bb commit da6e82f

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10390,7 +10390,8 @@ namespace ts {
1039010390
}
1039110391
else {
1039210392
error(func, Diagnostics.No_best_common_type_exists_among_return_expressions);
10393-
return unknownType;
10393+
// Defer to unioning the return types so we get a) downstream errors earlier and b) better Salsa experience
10394+
return getUnionType(types);
1039410395
}
1039510396
}
1039610397

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @allowNonTsExtensions: true
4+
// @Filename: file.js
5+
//// function fn() {
6+
//// if (foo) {
7+
//// return 0;
8+
//// } else {
9+
//// return '0';
10+
//// }
11+
//// }
12+
//// let x = fn();
13+
//// if(typeof x === 'string') {
14+
//// x/*str*/
15+
//// } else {
16+
//// x/*num*/
17+
//// }
18+
19+
goTo.marker('str');
20+
edit.insert('.');
21+
verify.completionListContains('substr', undefined, undefined, 'method');
22+
23+
goTo.marker('num');
24+
edit.insert('.');
25+
verify.completionListContains('toFixed', undefined, undefined, 'method');

0 commit comments

Comments
 (0)