@@ -2694,7 +2694,11 @@ namespace ts {
2694
2694
writePunctuation(writer, SyntaxKind.ColonToken);
2695
2695
writeSpace(writer);
2696
2696
2697
- buildTypeDisplay(getTypeOfSymbol(p), writer, enclosingDeclaration, flags, symbolStack);
2697
+ let type = getTypeOfSymbol(p);
2698
+ if (strictNullChecks && parameterNode.initializer && !(getModifierFlags(parameterNode) & ModifierFlags.ParameterPropertyModifier)) {
2699
+ type = includeFalsyTypes(type, TypeFlags.Undefined);
2700
+ }
2701
+ buildTypeDisplay(type, writer, enclosingDeclaration, flags, symbolStack);
2698
2702
}
2699
2703
2700
2704
function buildBindingPatternDisplay(bindingPattern: BindingPattern, writer: SymbolWriter, enclosingDeclaration?: Node, flags?: TypeFormatFlags, symbolStack?: Symbol[]) {
@@ -3315,9 +3319,7 @@ namespace ts {
3315
3319
3316
3320
// Use type from type annotation if one is present
3317
3321
if (declaration.type) {
3318
- const isOptional = declaration.questionToken ||
3319
- (declaration.initializer && declaration.kind === SyntaxKind.Parameter && !(getModifierFlags(declaration) & ModifierFlags.ParameterPropertyModifier));
3320
- return addOptionality(getTypeFromTypeNode(declaration.type), /*optional*/ isOptional && includeOptionality);
3322
+ return addOptionality(getTypeFromTypeNode(declaration.type), /*optional*/ declaration.questionToken && includeOptionality);
3321
3323
}
3322
3324
3323
3325
if ((compilerOptions.noImplicitAny || declaration.flags & NodeFlags.JavaScriptFile) &&
@@ -3368,10 +3370,7 @@ namespace ts {
3368
3370
// Use the type of the initializer expression if one is present
3369
3371
if (declaration.initializer) {
3370
3372
const type = checkDeclarationInitializer(declaration);
3371
- // initialized parameters (but not parameter properties) are optional
3372
- const isOptional = declaration.questionToken ||
3373
- (declaration.kind === SyntaxKind.Parameter && !(getModifierFlags(declaration) & ModifierFlags.ParameterPropertyModifier));
3374
- return addOptionality(type, isOptional && includeOptionality);
3373
+ return addOptionality(type, /*optional*/ declaration.questionToken && includeOptionality);
3375
3374
}
3376
3375
3377
3376
// If it is a short-hand property assignment, use the type of the identifier
@@ -7101,8 +7100,8 @@ namespace ts {
7101
7100
const sourceParams = source.parameters;
7102
7101
const targetParams = target.parameters;
7103
7102
for (let i = 0; i < checkCount; i++) {
7104
- const s = i < sourceMax ? getTypeOfSymbol (sourceParams[i]) : getRestTypeOfSignature(source);
7105
- const t = i < targetMax ? getTypeOfSymbol (targetParams[i]) : getRestTypeOfSignature(target);
7103
+ const s = i < sourceMax ? getTypeOfParameter (sourceParams[i]) : getRestTypeOfSignature(source);
7104
+ const t = i < targetMax ? getTypeOfParameter (targetParams[i]) : getRestTypeOfSignature(target);
7106
7105
const related = compareTypes(s, t, /*reportErrors*/ false) || compareTypes(t, s, reportErrors);
7107
7106
if (!related) {
7108
7107
if (reportErrors) {
@@ -8299,8 +8298,8 @@ namespace ts {
8299
8298
8300
8299
const targetLen = target.parameters.length;
8301
8300
for (let i = 0; i < targetLen; i++) {
8302
- const s = isRestParameterIndex(source, i) ? getRestTypeOfSignature(source) : getTypeOfSymbol (source.parameters[i]);
8303
- const t = isRestParameterIndex(target, i) ? getRestTypeOfSignature(target) : getTypeOfSymbol (target.parameters[i]);
8301
+ const s = isRestParameterIndex(source, i) ? getRestTypeOfSignature(source) : getTypeOfParameter (source.parameters[i]);
8302
+ const t = isRestParameterIndex(target, i) ? getRestTypeOfSignature(target) : getTypeOfParameter (target.parameters[i]);
8304
8303
const related = compareTypes(s, t);
8305
8304
if (!related) {
8306
8305
return Ternary.False;
@@ -9196,8 +9195,8 @@ namespace ts {
9196
9195
switch (source.kind) {
9197
9196
case SyntaxKind.Identifier:
9198
9197
return target.kind === SyntaxKind.Identifier && getResolvedSymbol(<Identifier>source) === getResolvedSymbol(<Identifier>target) ||
9199
- (target.kind === SyntaxKind.VariableDeclaration || target.kind === SyntaxKind.BindingElement || target.kind === SyntaxKind.Parameter ) &&
9200
- getExportSymbolOfValueSymbolIfExported(getResolvedSymbol(source as Identifier)) === getSymbolOfNode(target);
9198
+ ( target.kind === SyntaxKind.VariableDeclaration || target.kind === SyntaxKind.BindingElement ) &&
9199
+ getExportSymbolOfValueSymbolIfExported(getResolvedSymbol(< Identifier>source )) === getSymbolOfNode(target);
9201
9200
case SyntaxKind.ThisKeyword:
9202
9201
return target.kind === SyntaxKind.ThisKeyword;
9203
9202
case SyntaxKind.PropertyAccessExpression:
@@ -9291,13 +9290,6 @@ namespace ts {
9291
9290
return false;
9292
9291
}
9293
9292
9294
- function getInitializedParameterReducedType(declaredType: UnionType, assignedType: Type) {
9295
- if (declaredType === assignedType || getFalsyFlags(assignedType) & TypeFlags.Undefined) {
9296
- return declaredType;
9297
- }
9298
- return getTypeWithFacts(declaredType, TypeFacts.NEUndefined);
9299
- }
9300
-
9301
9293
// Remove those constituent types of declaredType to which no constituent type of assignedType is assignable.
9302
9294
// For example, when a variable of type number | string | boolean is assigned a value of type number | boolean,
9303
9295
// we remove type string.
@@ -9478,7 +9470,7 @@ namespace ts {
9478
9470
return links.resolvedType || getTypeOfExpression(node);
9479
9471
}
9480
9472
9481
- function getInitialTypeOfVariableDeclaration(node: VariableDeclaration | ParameterDeclaration ) {
9473
+ function getInitialTypeOfVariableDeclaration(node: VariableDeclaration) {
9482
9474
if (node.initializer) {
9483
9475
return getTypeOfInitializer(node.initializer);
9484
9476
}
@@ -9491,15 +9483,15 @@ namespace ts {
9491
9483
return unknownType;
9492
9484
}
9493
9485
9494
- function getInitialType(node: VariableDeclaration | BindingElement | ParameterDeclaration ) {
9495
- return node.kind === SyntaxKind.VariableDeclaration || node.kind === SyntaxKind.Parameter ?
9496
- getInitialTypeOfVariableDeclaration(<VariableDeclaration | ParameterDeclaration >node) :
9486
+ function getInitialType(node: VariableDeclaration | BindingElement) {
9487
+ return node.kind === SyntaxKind.VariableDeclaration ?
9488
+ getInitialTypeOfVariableDeclaration(<VariableDeclaration>node) :
9497
9489
getInitialTypeOfBindingElement(<BindingElement>node);
9498
9490
}
9499
9491
9500
- function getInitialOrAssignedType(node: VariableDeclaration | BindingElement | Expression | ParameterDeclaration ) {
9501
- return node.kind === SyntaxKind.VariableDeclaration || node.kind === SyntaxKind.BindingElement || node.kind === SyntaxKind.Parameter ?
9502
- getInitialType(<VariableDeclaration | BindingElement | ParameterDeclaration >node) :
9492
+ function getInitialOrAssignedType(node: VariableDeclaration | BindingElement | Expression) {
9493
+ return node.kind === SyntaxKind.VariableDeclaration || node.kind === SyntaxKind.BindingElement ?
9494
+ getInitialType(<VariableDeclaration | BindingElement>node) :
9503
9495
getAssignedType(<Expression>node);
9504
9496
}
9505
9497
@@ -9768,13 +9760,6 @@ namespace ts {
9768
9760
continue;
9769
9761
}
9770
9762
}
9771
- else if (flow.flags & FlowFlags.InitializedParameter) {
9772
- type = getTypeAtFlowInitializedParameter(flow as FlowInitializedParameter);
9773
- if (!type) {
9774
- flow = (flow as FlowInitializedParameter).antecedent;
9775
- continue;
9776
- }
9777
- }
9778
9763
else if (flow.flags & FlowFlags.Condition) {
9779
9764
type = getTypeAtFlowCondition(<FlowCondition>flow);
9780
9765
}
@@ -9822,20 +9807,6 @@ namespace ts {
9822
9807
}
9823
9808
}
9824
9809
9825
- function getTypeAtFlowInitializedParameter(flow: FlowInitializedParameter) {
9826
- const node = flow.node;
9827
- // Parameter initializers don't really narrow the declared type except to remove undefined.
9828
- // If the initializer includes undefined in the type, it doesn't even remove undefined.
9829
- if (isMatchingReference(reference, node)) {
9830
- if (declaredType.flags & TypeFlags.Union) {
9831
- return getInitializedParameterReducedType(<UnionType>declaredType, getInitialOrAssignedType(node));
9832
- }
9833
- return declaredType;
9834
- }
9835
-
9836
- return undefined;
9837
- }
9838
-
9839
9810
function getTypeAtFlowAssignment(flow: FlowAssignment) {
9840
9811
const node = flow.node;
9841
9812
// Assignments only narrow the computed type if the declared type is a union type. Thus, we
@@ -14033,10 +14004,21 @@ namespace ts {
14033
14004
}
14034
14005
}
14035
14006
14007
+ function getTypeOfParameter(symbol: Symbol) {
14008
+ const type = getTypeOfSymbol(symbol);
14009
+ if (strictNullChecks) {
14010
+ const declaration = symbol.valueDeclaration;
14011
+ if (declaration && (<VariableLikeDeclaration>declaration).initializer) {
14012
+ return includeFalsyTypes(type, TypeFlags.Undefined);
14013
+ }
14014
+ }
14015
+ return type;
14016
+ }
14017
+
14036
14018
function getTypeAtPosition(signature: Signature, pos: number): Type {
14037
14019
return signature.hasRestParameter ?
14038
- pos < signature.parameters.length - 1 ? getTypeOfSymbol (signature.parameters[pos]) : getRestTypeOfSignature(signature) :
14039
- pos < signature.parameters.length ? getTypeOfSymbol (signature.parameters[pos]) : anyType;
14020
+ pos < signature.parameters.length - 1 ? getTypeOfParameter (signature.parameters[pos]) : getRestTypeOfSignature(signature) :
14021
+ pos < signature.parameters.length ? getTypeOfParameter (signature.parameters[pos]) : anyType;
14040
14022
}
14041
14023
14042
14024
function assignContextualParameterTypes(signature: Signature, context: Signature, mapper: TypeMapper) {
@@ -20652,9 +20634,15 @@ namespace ts {
20652
20634
function writeTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter) {
20653
20635
// Get type of the symbol if this is the valid symbol otherwise get type at location
20654
20636
const symbol = getSymbolOfNode(declaration);
20655
- const type = symbol && !(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.Signature))
20637
+ let type = symbol && !(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.Signature))
20656
20638
? getWidenedLiteralType(getTypeOfSymbol(symbol))
20657
20639
: unknownType;
20640
+ if (strictNullChecks &&
20641
+ declaration.kind === SyntaxKind.Parameter &&
20642
+ (declaration as ParameterDeclaration).initializer &&
20643
+ !(getModifierFlags(declaration) & ModifierFlags.ParameterPropertyModifier)) {
20644
+ type = includeFalsyTypes(type, TypeFlags.Undefined);
20645
+ }
20658
20646
20659
20647
getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags);
20660
20648
}
0 commit comments