Skip to content

Commit da0f1e3

Browse files
committed
Ignore optionality when determining binding element parent type
1 parent d69f57b commit da0f1e3

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

src/compiler/checker.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,7 +2745,7 @@ namespace ts {
27452745
// assigned by contextual typing.
27462746
function getTypeForBindingElementParent(node: VariableLikeDeclaration) {
27472747
const symbol = getSymbolOfNode(node);
2748-
return symbol && getSymbolLinks(symbol).type || getTypeForVariableLikeDeclaration(node);
2748+
return symbol && getSymbolLinks(symbol).type || getTypeForVariableLikeDeclaration(node, /*includeOptionality*/ false);
27492749
}
27502750

27512751
function getTextOfPropertyName(name: PropertyName): string {
@@ -2885,7 +2885,7 @@ namespace ts {
28852885
}
28862886

28872887
// Return the inferred type for a variable, parameter, or property declaration
2888-
function getTypeForVariableLikeDeclaration(declaration: VariableLikeDeclaration): Type {
2888+
function getTypeForVariableLikeDeclaration(declaration: VariableLikeDeclaration, includeOptionality: boolean): Type {
28892889
if (declaration.flags & NodeFlags.JavaScriptFile) {
28902890
// If this is a variable in a JavaScript file, then use the JSDoc type (if it has
28912891
// one as its type), otherwise fallback to the below standard TS codepaths to
@@ -2915,7 +2915,7 @@ namespace ts {
29152915

29162916
// Use type from type annotation if one is present
29172917
if (declaration.type) {
2918-
return addOptionality(getTypeFromTypeNode(declaration.type), /*optional*/ !!declaration.questionToken);
2918+
return addOptionality(getTypeFromTypeNode(declaration.type), /*optional*/ declaration.questionToken && includeOptionality);
29192919
}
29202920

29212921
if (declaration.kind === SyntaxKind.Parameter) {
@@ -2937,13 +2937,13 @@ namespace ts {
29372937
? getContextuallyTypedThisType(func)
29382938
: getContextuallyTypedParameterType(<ParameterDeclaration>declaration);
29392939
if (type) {
2940-
return addOptionality(type, /*optional*/ !!declaration.questionToken);
2940+
return addOptionality(type, /*optional*/ declaration.questionToken && includeOptionality);
29412941
}
29422942
}
29432943

29442944
// Use the type of the initializer expression if one is present
29452945
if (declaration.initializer) {
2946-
return addOptionality(checkExpressionCached(declaration.initializer), /*optional*/ !!declaration.questionToken);
2946+
return addOptionality(checkExpressionCached(declaration.initializer), /*optional*/ declaration.questionToken && includeOptionality);
29472947
}
29482948

29492949
// If it is a short-hand property assignment, use the type of the identifier
@@ -3046,7 +3046,7 @@ namespace ts {
30463046
// binding pattern [x, s = ""]. Because the contextual type is a tuple type, the resulting type of [1, "one"] is the
30473047
// tuple type [number, string]. Thus, the type inferred for 'x' is number and the type inferred for 's' is string.
30483048
function getWidenedTypeForVariableLikeDeclaration(declaration: VariableLikeDeclaration, reportErrors?: boolean): Type {
3049-
let type = getTypeForVariableLikeDeclaration(declaration);
3049+
let type = getTypeForVariableLikeDeclaration(declaration, /*includeOptionality*/ true);
30503050
if (type) {
30513051
if (reportErrors) {
30523052
reportErrorsFromWidening(declaration, type);
@@ -16877,7 +16877,7 @@ namespace ts {
1687716877
}
1687816878

1687916879
if (isBindingPattern(node)) {
16880-
return getTypeForVariableLikeDeclaration(<VariableLikeDeclaration>node.parent);
16880+
return getTypeForVariableLikeDeclaration(<VariableLikeDeclaration>node.parent, /*includeOptionality*/ true);
1688116881
}
1688216882

1688316883
if (isInRightSideOfImportOrExportAssignment(<Identifier>node)) {

0 commit comments

Comments
 (0)