Union type instead of best common supertype for multiple return statements

This commit is contained in:
Anders Hejlsberg 2016-09-01 06:47:18 -07:00
parent d5c0c054be
commit b96f6cd84c
2 changed files with 2 additions and 22 deletions

View file

@ -12477,20 +12477,8 @@ namespace ts {
return isAsync ? createPromiseReturnType(func, voidType) : voidType;
}
}
// When yield/return statements are contextually typed we allow the return type to be a union type.
// Otherwise we require the yield/return expressions to have a best common supertype.
type = contextualSignature ? getUnionType(types, /*subtypeReduction*/ true) : getCommonSupertype(types);
if (!type) {
if (funcIsGenerator) {
error(func, Diagnostics.No_best_common_type_exists_among_yield_expressions);
return createIterableIteratorType(unknownType);
}
else {
error(func, Diagnostics.No_best_common_type_exists_among_return_expressions);
// Defer to unioning the return types so we get a) downstream errors earlier and b) better Salsa experience
return isAsync ? createPromiseReturnType(func, getUnionType(types, /*subtypeReduction*/ true)) : getUnionType(types, /*subtypeReduction*/ true);
}
}
// Return a union of the return expression types.
type = getUnionType(types, /*subtypeReduction*/ true);
if (funcIsGenerator) {
type = createIterableIteratorType(type);

View file

@ -1067,10 +1067,6 @@
"category": "Error",
"code": 2353
},
"No best common type exists among return expressions.": {
"category": "Error",
"code": 2354
},
"A function whose declared type is neither 'void' nor 'any' must return a value.": {
"category": "Error",
"code": 2355
@ -1635,10 +1631,6 @@
"category": "Error",
"code": 2503
},
"No best common type exists among yield expressions.": {
"category": "Error",
"code": 2504
},
"A generator cannot have a 'void' type annotation.": {
"category": "Error",
"code": 2505