@@ -2844,7 +2844,7 @@ namespace ts {
2844
2844
}
2845
2845
// In strict null checking mode, if a default value of a non-undefined type is specified, remove
2846
2846
// undefined from the final type.
2847
- if (strictNullChecks && declaration.initializer && !(getNullableKind (checkExpressionCached(declaration.initializer)) & TypeFlags.Undefined)) {
2847
+ if (strictNullChecks && declaration.initializer && !(getCombinedTypeFlags (checkExpressionCached(declaration.initializer)) & TypeFlags.Undefined)) {
2848
2848
type = getTypeWithFacts(type, TypeFacts.NEUndefined);
2849
2849
}
2850
2850
return type;
@@ -2887,7 +2887,7 @@ namespace ts {
2887
2887
}
2888
2888
2889
2889
function addOptionality(type: Type, optional: boolean): Type {
2890
- return strictNullChecks && optional ? addNullableKind (type, TypeFlags.Undefined) : type;
2890
+ return strictNullChecks && optional ? addTypeKind (type, TypeFlags.Undefined) : type;
2891
2891
}
2892
2892
2893
2893
// Return the inferred type for a variable, parameter, or property declaration
@@ -3222,7 +3222,7 @@ namespace ts {
3222
3222
if (!links.type) {
3223
3223
const type = createObjectType(TypeFlags.Anonymous, symbol);
3224
3224
links.type = strictNullChecks && symbol.flags & SymbolFlags.Optional ?
3225
- addNullableKind (type, TypeFlags.Undefined) : type;
3225
+ addTypeKind (type, TypeFlags.Undefined) : type;
3226
3226
}
3227
3227
return links.type;
3228
3228
}
@@ -6746,7 +6746,7 @@ namespace ts {
6746
6746
return getUnionType(types);
6747
6747
}
6748
6748
const supertype = forEach(primaryTypes, t => isSupertypeOfEach(t, primaryTypes) ? t : undefined);
6749
- return supertype && addNullableKind (supertype, getCombinedFlagsOfTypes(types) & TypeFlags.Nullable);
6749
+ return supertype && addTypeKind (supertype, getCombinedFlagsOfTypes(types) & TypeFlags.Nullable);
6750
6750
}
6751
6751
6752
6752
function reportNoCommonSupertypeError(types: Type[], errorLocation: Node, errorMessageChainHead: DiagnosticMessageChain): void {
@@ -6817,28 +6817,22 @@ namespace ts {
6817
6817
return !!(type.flags & TypeFlags.Tuple);
6818
6818
}
6819
6819
6820
- function getNullableKind(type: Type): TypeFlags {
6821
- let flags = type.flags;
6822
- if (flags & TypeFlags.Union) {
6823
- for (const t of (type as UnionType).types) {
6824
- flags |= t.flags;
6825
- }
6826
- }
6827
- return flags & TypeFlags.Nullable;
6820
+ function getCombinedTypeFlags(type: Type): TypeFlags {
6821
+ return type.flags & TypeFlags.Union ? getCombinedFlagsOfTypes((<UnionType>type).types) : type.flags;
6828
6822
}
6829
6823
6830
- function addNullableKind(type: Type, kind: TypeFlags): Type {
6831
- if ((getNullableKind(type) & kind) !== kind) {
6832
- const types = [type];
6833
- if (kind & TypeFlags.Undefined) {
6834
- types.push(undefinedType);
6835
- }
6836
- if (kind & TypeFlags.Null) {
6837
- types.push(nullType);
6838
- }
6839
- type = getUnionType(types);
6824
+ function addTypeKind(type: Type, kind: TypeFlags) {
6825
+ if ((getCombinedTypeFlags(type) & kind) === kind) {
6826
+ return type;
6840
6827
}
6841
- return type;
6828
+ const types = [type];
6829
+ if (kind & TypeFlags.String) types.push(stringType);
6830
+ if (kind & TypeFlags.Number) types.push(numberType);
6831
+ if (kind & TypeFlags.Boolean) types.push(booleanType);
6832
+ if (kind & TypeFlags.Void) types.push(voidType);
6833
+ if (kind & TypeFlags.Undefined) types.push(undefinedType);
6834
+ if (kind & TypeFlags.Null) types.push(nullType);
6835
+ return getUnionType(types);
6842
6836
}
6843
6837
6844
6838
function getNonNullableType(type: Type): Type {
@@ -7667,7 +7661,7 @@ namespace ts {
7667
7661
if (!reference.flowNode || assumeInitialized && !(declaredType.flags & TypeFlags.Narrowable)) {
7668
7662
return declaredType;
7669
7663
}
7670
- const initialType = assumeInitialized ? declaredType : addNullableKind (declaredType, TypeFlags.Undefined);
7664
+ const initialType = assumeInitialized ? declaredType : addTypeKind (declaredType, TypeFlags.Undefined);
7671
7665
const visitedFlowStart = visitedFlowCount;
7672
7666
const result = getTypeAtFlowNode(reference.flowNode);
7673
7667
visitedFlowCount = visitedFlowStart;
@@ -8172,7 +8166,7 @@ namespace ts {
8172
8166
getRootDeclaration(declaration).kind === SyntaxKind.Parameter || isInAmbientContext(declaration) ||
8173
8167
!isDeclarationIncludedInFlow(node, declaration, includeOuterFunctions);
8174
8168
const flowType = getFlowTypeOfReference(node, type, assumeInitialized, includeOuterFunctions);
8175
- if (!assumeInitialized && !(getNullableKind (type) & TypeFlags.Undefined) && getNullableKind (flowType) & TypeFlags.Undefined) {
8169
+ if (!assumeInitialized && !(getCombinedTypeFlags (type) & TypeFlags.Undefined) && getCombinedTypeFlags (flowType) & TypeFlags.Undefined) {
8176
8170
error(node, Diagnostics.Variable_0_is_used_before_being_assigned, symbolToString(symbol));
8177
8171
// Return the declared type to reduce follow-on errors
8178
8172
return type;
@@ -9954,7 +9948,7 @@ namespace ts {
9954
9948
function checkNonNullExpression(node: Expression | QualifiedName) {
9955
9949
const type = checkExpression(node);
9956
9950
if (strictNullChecks) {
9957
- const kind = getNullableKind (type);
9951
+ const kind = getCombinedTypeFlags (type) & TypeFlags.Nullable ;
9958
9952
if (kind) {
9959
9953
error(node, kind & TypeFlags.Undefined ? kind & TypeFlags.Null ?
9960
9954
Diagnostics.Object_is_possibly_null_or_undefined :
@@ -11488,7 +11482,7 @@ namespace ts {
11488
11482
if (strictNullChecks) {
11489
11483
const declaration = symbol.valueDeclaration;
11490
11484
if (declaration && (<VariableLikeDeclaration>declaration).initializer) {
11491
- return addNullableKind (type, TypeFlags.Undefined);
11485
+ return addTypeKind (type, TypeFlags.Undefined);
11492
11486
}
11493
11487
}
11494
11488
return type;
@@ -12414,7 +12408,7 @@ namespace ts {
12414
12408
case SyntaxKind.InKeyword:
12415
12409
return checkInExpression(left, right, leftType, rightType);
12416
12410
case SyntaxKind.AmpersandAmpersandToken:
12417
- return strictNullChecks ? addNullableKind (rightType, getNullableKind (leftType)) : rightType;
12411
+ return strictNullChecks ? addTypeKind (rightType, getCombinedTypeFlags (leftType) & TypeFlags.Falsy ) : rightType;
12418
12412
case SyntaxKind.BarBarToken:
12419
12413
return getUnionType([getNonNullableType(leftType), rightType]);
12420
12414
case SyntaxKind.EqualsToken:
0 commit comments