mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
Revert "Add related diagnostic to "used before defined" if type is a function that returns a union with undefined (#33171)" (#35751)
This reverts commit be960fa356.
This commit is contained in:
@@ -20418,26 +20418,7 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
else if (!assumeInitialized && !(getFalsyFlags(type) & TypeFlags.Undefined) && getFalsyFlags(flowType) & TypeFlags.Undefined) {
|
||||
const diag = error(node, Diagnostics.Variable_0_is_used_before_being_assigned, symbolToString(symbol));
|
||||
|
||||
// See GH:32846 - if the user is using a variable whose type is () => T1 | ... | undefined
|
||||
// they may have meant to specify the type as (() => T1 | ...) | undefined
|
||||
// This is assumed if: the type is a FunctionType, the return type is a Union, the last constituent of
|
||||
// the union is `undefined`
|
||||
if (type.symbol && type.symbol.declarations.length === 1 && isFunctionTypeNode(type.symbol.declarations[0])) {
|
||||
const funcTypeNode = <FunctionTypeNode>type.symbol.declarations[0];
|
||||
const returnType = getReturnTypeFromAnnotation(funcTypeNode);
|
||||
if (returnType && returnType.flags & TypeFlags.Union) {
|
||||
const unionTypes = (<UnionTypeNode>funcTypeNode.type).types;
|
||||
if (unionTypes && unionTypes[unionTypes.length - 1].kind === SyntaxKind.UndefinedKeyword) {
|
||||
const parenedFuncType = getMutableClone(funcTypeNode);
|
||||
// Highlight to the end of the second to last constituent of the union
|
||||
parenedFuncType.end = unionTypes[unionTypes.length - 2].end;
|
||||
addRelatedInfo(diag, createDiagnosticForNode(parenedFuncType, Diagnostics.Did_you_mean_to_parenthesize_this_function_type));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
error(node, Diagnostics.Variable_0_is_used_before_being_assigned, symbolToString(symbol));
|
||||
// Return the declared type to reduce follow-on errors
|
||||
return type;
|
||||
}
|
||||
|
||||
@@ -1119,7 +1119,6 @@
|
||||
"category": "Error",
|
||||
"code": 1375
|
||||
},
|
||||
|
||||
"The types of '{0}' are incompatible between these types.": {
|
||||
"category": "Error",
|
||||
"code": 2200
|
||||
|
||||
Reference in New Issue
Block a user