Skip to content

Commit 503dacc

Browse files
committed
Ensure computation does not involved declaredType
1 parent b2d122d commit 503dacc

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/compiler/checker.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ const enum TypeSystemPropertyName {
12331233
ResolvedTypeArguments,
12341234
ResolvedBaseTypes,
12351235
WriteType,
1236-
IsParameterWithUndefinedInAnnotation,
1236+
ParameterInitializerContainsUndefined,
12371237
}
12381238

12391239
/** @internal */
@@ -9983,8 +9983,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
99839983
return !!(target as InterfaceType).baseTypesResolved;
99849984
case TypeSystemPropertyName.WriteType:
99859985
return !!getSymbolLinks(target as Symbol).writeType;
9986-
case TypeSystemPropertyName.IsParameterWithUndefinedInAnnotation:
9987-
return getNodeLinks(target as VariableDeclaration).isParameterWithUndefinedInAnnotation !== undefined;
9986+
case TypeSystemPropertyName.ParameterInitializerContainsUndefined:
9987+
return getNodeLinks(target as ParameterDeclaration).parameterInitializerContainsUndefined !== undefined;
99889988
}
99899989
return Debug.assertNever(propertyName);
99909990
}
@@ -27295,31 +27295,37 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2729527295
return symbol.flags & SymbolFlags.Variable && (getDeclarationNodeFlagsFromSymbol(symbol) & NodeFlags.Const) !== 0;
2729627296
}
2729727297

27298-
/** remove undefined from the annotated type of a parameter when there is an initializer (that doesn't include undefined) */
27299-
function removeOptionalityFromDeclaredType(declaredType: Type, declaration: VariableLikeDeclaration): Type {
27298+
function parameterInitializerContainsUndefined(declaration: ParameterDeclaration): boolean {
2730027299
const links = getNodeLinks(declaration);
2730127300

27302-
if (links.isParameterWithUndefinedInAnnotation === undefined) {
27303-
if (!pushTypeResolution(declaration, TypeSystemPropertyName.IsParameterWithUndefinedInAnnotation)) {
27301+
if (links.parameterInitializerContainsUndefined === undefined) {
27302+
if (!pushTypeResolution(declaration, TypeSystemPropertyName.ParameterInitializerContainsUndefined)) {
2730427303
reportCircularityError(declaration.symbol);
27305-
return declaredType;
27304+
return true;
2730627305
}
2730727306

27308-
const annotationIncludesUndefined = strictNullChecks &&
27309-
declaration.kind === SyntaxKind.Parameter &&
27310-
declaration.initializer &&
27311-
getTypeFacts(declaredType) & TypeFacts.IsUndefined &&
27312-
!(getTypeFacts(checkDeclarationInitializer(declaration, CheckMode.Normal)) & TypeFacts.IsUndefined);
27307+
const containsUndefined = !!(getTypeFacts(checkDeclarationInitializer(declaration, CheckMode.Normal)) & TypeFacts.IsUndefined);
2731327308

2731427309
if (!popTypeResolution()) {
2731527310
reportCircularityError(declaration.symbol);
27316-
return declaredType;
27311+
return true;
2731727312
}
2731827313

27319-
links.isParameterWithUndefinedInAnnotation = !!annotationIncludesUndefined;
27314+
links.parameterInitializerContainsUndefined = containsUndefined;
2732027315
}
2732127316

27322-
return links.isParameterWithUndefinedInAnnotation ? getTypeWithFacts(declaredType, TypeFacts.NEUndefined) : declaredType;
27317+
return links.parameterInitializerContainsUndefined;
27318+
}
27319+
27320+
/** remove undefined from the annotated type of a parameter when there is an initializer (that doesn't include undefined) */
27321+
function removeOptionalityFromDeclaredType(declaredType: Type, declaration: VariableLikeDeclaration): Type {
27322+
const removeUndefined = strictNullChecks &&
27323+
declaration.kind === SyntaxKind.Parameter &&
27324+
declaration.initializer &&
27325+
getTypeFacts(declaredType) & TypeFacts.IsUndefined &&
27326+
!parameterInitializerContainsUndefined(declaration);
27327+
27328+
return removeUndefined ? getTypeWithFacts(declaredType, TypeFacts.NEUndefined) : declaredType;
2732327329
}
2732427330

2732527331
function isConstraintPosition(type: Type, node: Node) {

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5997,7 +5997,7 @@ export interface NodeLinks {
59975997
declarationRequiresScopeChange?: boolean; // Set by `useOuterVariableScopeInParameter` in checker when downlevel emit would change the name resolution scope inside of a parameter.
59985998
serializedTypes?: Map<string, SerializedTypeEntry>; // Collection of types serialized at this location
59995999
decoratorSignature?: Signature; // Signature for decorator as if invoked by the runtime.
6000-
isParameterWithUndefinedInAnnotation?: boolean; // True if this is a parameter declaration whose type annotation contains "undefined".
6000+
parameterInitializerContainsUndefined?: boolean; // True if this is a parameter declaration whose type annotation contains "undefined".
60016001
}
60026002

60036003
/** @internal */

0 commit comments

Comments
 (0)