diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 61c05d7688c85..55587ca683cbd 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10390,7 +10390,8 @@ namespace ts { } else { error(func, Diagnostics.No_best_common_type_exists_among_return_expressions); - return unknownType; + // Defer to unioning the return types so we get a) downstream errors earlier and b) better Salsa experience + return getUnionType(types); } } diff --git a/tests/cases/fourslash/getJavaScriptCompletions19.ts b/tests/cases/fourslash/getJavaScriptCompletions19.ts new file mode 100644 index 0000000000000..5a361930e86b2 --- /dev/null +++ b/tests/cases/fourslash/getJavaScriptCompletions19.ts @@ -0,0 +1,25 @@ +/// + +// @allowNonTsExtensions: true +// @Filename: file.js +//// function fn() { +//// if (foo) { +//// return 0; +//// } else { +//// return '0'; +//// } +//// } +//// let x = fn(); +//// if(typeof x === 'string') { +//// x/*str*/ +//// } else { +//// x/*num*/ +//// } + +goTo.marker('str'); +edit.insert('.'); +verify.completionListContains('substr', undefined, undefined, 'method'); + +goTo.marker('num'); +edit.insert('.'); +verify.completionListContains('toFixed', undefined, undefined, 'method');