Skip to content

Commit de2ef72

Browse files
RyanCavanaughbillti
authored andcommitted
Use union types in the return type of functions in the error case
Fixes #6663 (cherry picked from commit da6e82f)
1 parent 671d83e commit de2ef72

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
@@ -10433,7 +10433,8 @@ namespace ts {
1043310433
}
1043410434
else {
1043510435
error(func, Diagnostics.No_best_common_type_exists_among_return_expressions);
10436-
return unknownType;
10436+
// Defer to unioning the return types so we get a) downstream errors earlier and b) better Salsa experience
10437+
return getUnionType(types);
1043710438
}
1043810439
}
1043910440

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)