Skip to content

Commit 5f4a03c

Browse files
author
Andy
authored
Ensure TypeChecker#getTypeAtLocation returns a defined result (#25583)
* Ensure TypeChecker#getTypeAtLocation returns a defined result * Update additional baseline
1 parent 2d0d655 commit 5f4a03c

16 files changed

+37
-37
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18834,7 +18834,7 @@ namespace ts {
1883418834
node.kind === SyntaxKind.SetAccessor) {
1883518835
// The `descriptor` for a method decorator will be a `TypedPropertyDescriptor<T>`
1883618836
// for the type of the member.
18837-
const propertyType = getTypeOfNode(node)!; // TODO: GH#18217
18837+
const propertyType = getTypeOfNode(node);
1883818838
return createTypedPropertyDescriptorType(propertyType);
1883918839
}
1884018840

@@ -21849,7 +21849,7 @@ namespace ts {
2184921849
else {
2185021850
const leadingError = () => chainDiagnosticMessages(/*details*/ undefined, Diagnostics.A_type_predicate_s_type_must_be_assignable_to_its_parameter_s_type);
2185121851
checkTypeAssignableTo(typePredicate.type,
21852-
getTypeOfNode(parent.parameters[typePredicate.parameterIndex])!, // TODO: GH#18217
21852+
getTypeOfNode(parent.parameters[typePredicate.parameterIndex]),
2185321853
node.type,
2185421854
/*headMessage*/ undefined,
2185521855
leadingError);
@@ -23181,7 +23181,7 @@ namespace ts {
2318123181
case SyntaxKind.MethodDeclaration:
2318223182
case SyntaxKind.GetAccessor:
2318323183
case SyntaxKind.SetAccessor:
23184-
const methodType = getTypeOfNode(node.parent)!; // TODO: GH#18217
23184+
const methodType = getTypeOfNode(node.parent);
2318523185
const descriptorType = createTypedPropertyDescriptorType(methodType);
2318623186
expectedReturnType = getUnionType([descriptorType, voidType]);
2318723187
break;
@@ -27120,7 +27120,7 @@ namespace ts {
2712027120
resolveEntityName(node.propertyName || node.name, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace | SymbolFlags.Alias);
2712127121
}
2712227122

27123-
function getTypeOfNode(node: Node): Type | undefined {
27123+
function getTypeOfNode(node: Node): Type {
2712427124
if (node.flags & NodeFlags.InWithStatement) {
2712527125
// We cannot answer semantic questions within a with block, do not proceed any further
2712627126
return errorType;
@@ -27159,7 +27159,7 @@ namespace ts {
2715927159

2716027160
if (isTypeDeclarationName(node)) {
2716127161
const symbol = getSymbolAtLocation(node);
27162-
return symbol && getDeclaredTypeOfSymbol(symbol);
27162+
return symbol ? getDeclaredTypeOfSymbol(symbol) : errorType;
2716327163
}
2716427164

2716527165
if (isDeclaration(node)) {
@@ -27170,11 +27170,11 @@ namespace ts {
2717027170

2717127171
if (isDeclarationNameOrImportPropertyName(node)) {
2717227172
const symbol = getSymbolAtLocation(node);
27173-
return symbol && getTypeOfSymbol(symbol);
27173+
return symbol ? getTypeOfSymbol(symbol) : errorType;
2717427174
}
2717527175

2717627176
if (isBindingPattern(node)) {
27177-
return getTypeForVariableLikeDeclaration(node.parent, /*includeOptionality*/ true);
27177+
return getTypeForVariableLikeDeclaration(node.parent, /*includeOptionality*/ true) || errorType;
2717827178
}
2717927179

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

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2936,7 +2936,7 @@ namespace ts {
29362936
*/
29372937
getExportSymbolOfSymbol(symbol: Symbol): Symbol;
29382938
getPropertySymbolOfDestructuringAssignment(location: Identifier): Symbol | undefined;
2939-
getTypeAtLocation(node: Node): Type | undefined;
2939+
getTypeAtLocation(node: Node): Type;
29402940
getTypeFromTypeNode(node: TypeNode): Type;
29412941

29422942
signatureToString(signature: Signature, enclosingDeclaration?: Node, flags?: TypeFormatFlags, kind?: SignatureKind): string;

src/services/codefixes/fixAddMissingMember.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ namespace ts.codefix {
124124
const { parent } = token;
125125
if (!isPropertyAccessExpression(parent)) return undefined;
126126

127-
const leftExpressionType = skipConstraint(checker.getTypeAtLocation(parent.expression)!);
127+
const leftExpressionType = skipConstraint(checker.getTypeAtLocation(parent.expression));
128128
const { symbol } = leftExpressionType;
129129
if (!symbol || !symbol.declarations) return undefined;
130130

@@ -183,7 +183,7 @@ namespace ts.codefix {
183183
if (token.parent.parent.kind === SyntaxKind.BinaryExpression) {
184184
const binaryExpression = token.parent.parent as BinaryExpression;
185185
const otherExpression = token.parent === binaryExpression.left ? binaryExpression.right : binaryExpression.left;
186-
const widenedType = checker.getWidenedType(checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(otherExpression)!)); // TODO: GH#18217
186+
const widenedType = checker.getWidenedType(checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(otherExpression)));
187187
typeNode = checker.typeToTypeNode(widenedType, classDeclaration);
188188
}
189189
return typeNode || createKeywordTypeNode(SyntaxKind.AnyKeyword);

src/services/codefixes/fixClassDoesntImplementInheritedAbstractMember.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace ts.codefix {
3434

3535
function addMissingMembers(classDeclaration: ClassLikeDeclaration, sourceFile: SourceFile, checker: TypeChecker, changeTracker: textChanges.ChangeTracker, preferences: UserPreferences): void {
3636
const extendsNode = getEffectiveBaseTypeNode(classDeclaration)!;
37-
const instantiatedExtendsType = checker.getTypeAtLocation(extendsNode)!;
37+
const instantiatedExtendsType = checker.getTypeAtLocation(extendsNode);
3838

3939
// Note that this is ultimately derived from a map indexed by symbol names,
4040
// so duplicates cannot occur.

src/services/codefixes/fixClassIncorrectlyImplementsInterface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace ts.codefix {
5151
const implementedTypeSymbols = checker.getPropertiesOfType(implementedType);
5252
const nonPrivateAndNotExistedInHeritageClauseMembers = implementedTypeSymbols.filter(and(symbolPointsToNonPrivateMember, symbol => !maybeHeritageClauseSymbol.has(symbol.escapedName)));
5353

54-
const classType = checker.getTypeAtLocation(classDeclaration)!;
54+
const classType = checker.getTypeAtLocation(classDeclaration);
5555

5656
if (!classType.getNumberIndexType()) {
5757
createMissingIndexSignatureDeclaration(implementedType, IndexKind.Number);

src/services/codefixes/fixInvalidImportSyntax.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ namespace ts.codefix {
7777
}
7878

7979
function getImportCodeFixesForExpression(context: CodeFixContext, expr: Node): CodeFixAction[] | undefined {
80-
const type = context.program.getTypeChecker().getTypeAtLocation(expr)!; // TODO: GH#18217
80+
const type = context.program.getTypeChecker().getTypeAtLocation(expr);
8181
if (!(type.symbol && (type.symbol as TransientSymbol).originatingImport)) {
8282
return [];
8383
}

src/services/codefixes/fixSpelling.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace ts.codefix {
3535
let suggestion: string | undefined;
3636
if (isPropertyAccessExpression(node.parent) && node.parent.name === node) {
3737
Debug.assert(node.kind === SyntaxKind.Identifier);
38-
const containingType = checker.getTypeAtLocation(node.parent.expression)!;
38+
const containingType = checker.getTypeAtLocation(node.parent.expression);
3939
suggestion = checker.getSuggestionForNonexistentProperty(node as Identifier, containingType);
4040
}
4141
else if (isImportSpecifier(node.parent) && node.parent.name === node) {

src/services/codefixes/inferFromUsage.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ namespace ts.codefix {
401401
case SyntaxKind.LessThanEqualsToken:
402402
case SyntaxKind.GreaterThanToken:
403403
case SyntaxKind.GreaterThanEqualsToken:
404-
const operandType = checker.getTypeAtLocation(parent.left === node ? parent.right : parent.left)!;
404+
const operandType = checker.getTypeAtLocation(parent.left === node ? parent.right : parent.left);
405405
if (operandType.flags & TypeFlags.EnumLike) {
406406
addCandidateType(usageContext, operandType);
407407
}
@@ -412,7 +412,7 @@ namespace ts.codefix {
412412

413413
case SyntaxKind.PlusEqualsToken:
414414
case SyntaxKind.PlusToken:
415-
const otherOperandType = checker.getTypeAtLocation(parent.left === node ? parent.right : parent.left)!;
415+
const otherOperandType = checker.getTypeAtLocation(parent.left === node ? parent.right : parent.left);
416416
if (otherOperandType.flags & TypeFlags.EnumLike) {
417417
addCandidateType(usageContext, otherOperandType);
418418
}
@@ -472,7 +472,7 @@ namespace ts.codefix {
472472

473473
if (parent.arguments) {
474474
for (const argument of parent.arguments) {
475-
callContext.argumentTypes.push(checker.getTypeAtLocation(argument)!);
475+
callContext.argumentTypes.push(checker.getTypeAtLocation(argument));
476476
}
477477
}
478478

@@ -501,7 +501,7 @@ namespace ts.codefix {
501501
return;
502502
}
503503
else {
504-
const indexType = checker.getTypeAtLocation(parent)!;
504+
const indexType = checker.getTypeAtLocation(parent);
505505
const indexUsageContext = {};
506506
inferTypeFromContext(parent, checker, indexUsageContext);
507507
if (indexType.flags & TypeFlags.NumberLike) {

src/services/completions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ namespace ts.Completions {
10981098
}
10991099

11001100
if (!isTypeLocation) {
1101-
addTypeProperties(typeChecker.getTypeAtLocation(node)!);
1101+
addTypeProperties(typeChecker.getTypeAtLocation(node));
11021102
}
11031103
}
11041104

src/services/goToDefinition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ namespace ts.GoToDefinition {
171171
// At 'x.foo', see if the type of 'x' has an index signature, and if so find its declarations.
172172
function getDefinitionInfoForIndexSignatures(node: Node, checker: TypeChecker): DefinitionInfo[] | undefined {
173173
if (!isPropertyAccessExpression(node.parent) || node.parent.name !== node) return;
174-
const type = checker.getTypeAtLocation(node.parent.expression)!;
174+
const type = checker.getTypeAtLocation(node.parent.expression);
175175
return mapDefined(type.isUnionOrIntersection() ? type.types : [type], nonUnionType => {
176176
const info = checker.getIndexInfoOfType(nonUnionType, IndexKind.String);
177177
return info && info.declaration && createDefinitionFromSignatureDeclaration(checker, info.declaration);

0 commit comments

Comments
 (0)