Skip to content

Commit 3da069c

Browse files
RyanCavanaughtimsuchanek
authored andcommitted
small refactor to cut back on type assertions (microsoft#32920)
1 parent a16d11e commit 3da069c

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

src/compiler/checker.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,11 @@ namespace ts {
381381
typeToTypeNode: nodeBuilder.typeToTypeNode,
382382
indexInfoToIndexSignatureDeclaration: nodeBuilder.indexInfoToIndexSignatureDeclaration,
383383
signatureToSignatureDeclaration: nodeBuilder.signatureToSignatureDeclaration,
384-
symbolToEntityName: nodeBuilder.symbolToEntityName as (symbol: Symbol, meaning: SymbolFlags, enclosingDeclaration?: Node, flags?: NodeBuilderFlags) => EntityName, // TODO: GH#18217
385-
symbolToExpression: nodeBuilder.symbolToExpression as (symbol: Symbol, meaning: SymbolFlags, enclosingDeclaration?: Node, flags?: NodeBuilderFlags) => Expression, // TODO: GH#18217
384+
symbolToEntityName: nodeBuilder.symbolToEntityName,
385+
symbolToExpression: nodeBuilder.symbolToExpression,
386386
symbolToTypeParameterDeclarations: nodeBuilder.symbolToTypeParameterDeclarations,
387-
symbolToParameterDeclaration: nodeBuilder.symbolToParameterDeclaration as (symbol: Symbol, enclosingDeclaration?: Node, flags?: NodeBuilderFlags) => ParameterDeclaration, // TODO: GH#18217
388-
typeParameterToDeclaration: nodeBuilder.typeParameterToDeclaration as (parameter: TypeParameter, enclosingDeclaration?: Node, flags?: NodeBuilderFlags) => TypeParameterDeclaration, // TODO: GH#18217
387+
symbolToParameterDeclaration: nodeBuilder.symbolToParameterDeclaration,
388+
typeParameterToDeclaration: nodeBuilder.typeParameterToDeclaration,
389389
getSymbolsInScope: (location, meaning) => {
390390
location = getParseTreeNode(location);
391391
return location ? getSymbolsInScope(location, meaning) : [];
@@ -554,7 +554,7 @@ namespace ts {
554554
},
555555
getJsxNamespace: n => unescapeLeadingUnderscores(getJsxNamespace(n)),
556556
getAccessibleSymbolChain,
557-
getTypePredicateOfSignature: getTypePredicateOfSignature as (signature: Signature) => TypePredicate, // TODO: GH#18217
557+
getTypePredicateOfSignature,
558558
resolveExternalModuleSymbol,
559559
tryGetThisTypeAt: (node, includeGlobalThis) => {
560560
node = getParseTreeNode(node);
@@ -881,7 +881,7 @@ namespace ts {
881881
}
882882
const jsxPragma = file.pragmas.get("jsx");
883883
if (jsxPragma) {
884-
const chosenpragma: any = isArray(jsxPragma) ? jsxPragma[0] : jsxPragma; // TODO: GH#18217
884+
const chosenpragma = isArray(jsxPragma) ? jsxPragma[0] : jsxPragma;
885885
file.localJsxFactory = parseIsolatedEntityName(chosenpragma.arguments.factory, languageVersion);
886886
if (file.localJsxFactory) {
887887
return file.localJsxNamespace = getFirstIdentifier(file.localJsxFactory).escapedText;
@@ -2562,10 +2562,6 @@ namespace ts {
25622562
}
25632563

25642564
function resolveExternalModule(location: Node, moduleReference: string, moduleNotFoundError: DiagnosticMessage | undefined, errorNode: Node, isForAugmentation = false): Symbol | undefined {
2565-
if (moduleReference === undefined) {
2566-
return;
2567-
}
2568-
25692565
if (startsWith(moduleReference, "@types/")) {
25702566
const diag = Diagnostics.Cannot_import_type_declaration_files_Consider_importing_0_instead_of_1;
25712567
const withoutAtTypePrefix = removePrefix(moduleReference, "@types/");
@@ -3263,12 +3259,12 @@ namespace ts {
32633259
return access.accessibility === SymbolAccessibility.Accessible;
32643260
}
32653261

3266-
function isValueSymbolAccessible(typeSymbol: Symbol, enclosingDeclaration: Node): boolean {
3262+
function isValueSymbolAccessible(typeSymbol: Symbol, enclosingDeclaration: Node | undefined): boolean {
32673263
const access = isSymbolAccessible(typeSymbol, enclosingDeclaration, SymbolFlags.Value, /*shouldComputeAliasesToMakeVisible*/ false);
32683264
return access.accessibility === SymbolAccessibility.Accessible;
32693265
}
32703266

3271-
function isAnySymbolAccessible(symbols: Symbol[] | undefined, enclosingDeclaration: Node, initialSymbol: Symbol, meaning: SymbolFlags, shouldComputeAliasesToMakeVisible: boolean): SymbolAccessibilityResult | undefined {
3267+
function isAnySymbolAccessible(symbols: Symbol[] | undefined, enclosingDeclaration: Node | undefined, initialSymbol: Symbol, meaning: SymbolFlags, shouldComputeAliasesToMakeVisible: boolean): SymbolAccessibilityResult | undefined {
32723268
if (!length(symbols)) return;
32733269

32743270
let hadAccessibleChain: Symbol | undefined;
@@ -3542,7 +3538,7 @@ namespace ts {
35423538
typeToTypeNode: (type: Type, enclosingDeclaration?: Node, flags?: NodeBuilderFlags, tracker?: SymbolTracker) =>
35433539
withContext(enclosingDeclaration, flags, tracker, context => typeToTypeNodeHelper(type, context)),
35443540
indexInfoToIndexSignatureDeclaration: (indexInfo: IndexInfo, kind: IndexKind, enclosingDeclaration?: Node, flags?: NodeBuilderFlags, tracker?: SymbolTracker) =>
3545-
withContext(enclosingDeclaration, flags, tracker, context => indexInfoToIndexSignatureDeclarationHelper(indexInfo, kind, context))!, // TODO: GH#18217
3541+
withContext(enclosingDeclaration, flags, tracker, context => indexInfoToIndexSignatureDeclarationHelper(indexInfo, kind, context)),
35463542
signatureToSignatureDeclaration: (signature: Signature, kind: SyntaxKind, enclosingDeclaration?: Node, flags?: NodeBuilderFlags, tracker?: SymbolTracker) =>
35473543
withContext(enclosingDeclaration, flags, tracker, context => signatureToSignatureDeclarationHelper(signature, kind, context)),
35483544
symbolToEntityName: (symbol: Symbol, meaning: SymbolFlags, enclosingDeclaration?: Node, flags?: NodeBuilderFlags, tracker?: SymbolTracker) =>
@@ -3651,7 +3647,7 @@ namespace ts {
36513647
}
36523648
if (type.flags & TypeFlags.UniqueESSymbol) {
36533649
if (!(context.flags & NodeBuilderFlags.AllowUniqueESSymbolType)) {
3654-
if (isValueSymbolAccessible(type.symbol, context.enclosingDeclaration!)) {
3650+
if (isValueSymbolAccessible(type.symbol, context.enclosingDeclaration)) {
36553651
context.approximateLength += 6;
36563652
return symbolToTypeNode(type.symbol, context, SymbolFlags.Value);
36573653
}
@@ -3862,7 +3858,7 @@ namespace ts {
38623858
if (isStaticMethodSymbol || isNonLocalFunctionSymbol) {
38633859
// typeof is allowed only for static/non local functions
38643860
return (!!(context.flags & NodeBuilderFlags.UseTypeOfFunction) || (context.visitedTypes && context.visitedTypes.has(typeId))) && // it is type of the symbol uses itself recursively
3865-
(!(context.flags & NodeBuilderFlags.UseStructuralFallback) || isValueSymbolAccessible(symbol, context.enclosingDeclaration!)); // TODO: GH#18217 // And the build is going to succeed without visibility error or there is no structural fallback allowed
3861+
(!(context.flags & NodeBuilderFlags.UseStructuralFallback) || isValueSymbolAccessible(symbol, context.enclosingDeclaration)); // And the build is going to succeed without visibility error or there is no structural fallback allowed
38663862
}
38673863
}
38683864
}
@@ -3939,7 +3935,7 @@ namespace ts {
39393935
else if (context.flags & NodeBuilderFlags.WriteClassExpressionAsTypeLiteral &&
39403936
type.symbol.valueDeclaration &&
39413937
isClassLike(type.symbol.valueDeclaration) &&
3942-
!isValueSymbolAccessible(type.symbol, context.enclosingDeclaration!)
3938+
!isValueSymbolAccessible(type.symbol, context.enclosingDeclaration)
39433939
) {
39443940
return createAnonymousTypeNode(type);
39453941
}
@@ -7155,7 +7151,7 @@ namespace ts {
71557151
const baseConstructorType = getBaseConstructorTypeOfClass(classType);
71567152
const baseSignatures = getSignaturesOfType(baseConstructorType, SignatureKind.Construct);
71577153
if (baseSignatures.length === 0) {
7158-
return [createSignature(undefined, classType.localTypeParameters, undefined, emptyArray, classType, /*resolvedTypePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false)]; // TODO: GH#18217
7154+
return [createSignature(undefined, classType.localTypeParameters, undefined, emptyArray, classType, /*resolvedTypePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false)];
71597155
}
71607156
const baseTypeNode = getBaseTypeNodeOfClass(classType)!;
71617157
const isJavaScript = isInJSFile(baseTypeNode);
@@ -10224,9 +10220,11 @@ namespace ts {
1022410220
case SyntaxKind.ReadonlyKeyword:
1022510221
links.resolvedType = getTypeFromTypeNode(node.type);
1022610222
break;
10223+
default:
10224+
throw Debug.assertNever(node.operator);
1022710225
}
1022810226
}
10229-
return links.resolvedType!; // TODO: GH#18217
10227+
return links.resolvedType;
1023010228
}
1023110229

1023210230
function createIndexedAccessType(objectType: Type, indexType: Type) {
@@ -10811,11 +10809,11 @@ namespace ts {
1081110809
getNodeLinks(current.parent).resolvedSymbol = next;
1081210810
currentNamespace = next;
1081310811
}
10814-
resolveImportSymbolType(node, links, currentNamespace, targetMeaning);
10812+
links.resolvedType = resolveImportSymbolType(node, links, currentNamespace, targetMeaning);
1081510813
}
1081610814
else {
1081710815
if (moduleSymbol.flags & targetMeaning) {
10818-
resolveImportSymbolType(node, links, moduleSymbol, targetMeaning);
10816+
links.resolvedType = resolveImportSymbolType(node, links, moduleSymbol, targetMeaning);
1081910817
}
1082010818
else {
1082110819
const errorMessage = targetMeaning === SymbolFlags.Value
@@ -10829,17 +10827,17 @@ namespace ts {
1082910827
}
1083010828
}
1083110829
}
10832-
return links.resolvedType!; // TODO: GH#18217
10830+
return links.resolvedType;
1083310831
}
1083410832

1083510833
function resolveImportSymbolType(node: ImportTypeNode, links: NodeLinks, symbol: Symbol, meaning: SymbolFlags) {
1083610834
const resolvedSymbol = resolveSymbol(symbol);
1083710835
links.resolvedSymbol = resolvedSymbol;
1083810836
if (meaning === SymbolFlags.Value) {
10839-
return links.resolvedType = getTypeOfSymbol(symbol); // intentionally doesn't use resolved symbol so type is cached as expected on the alias
10837+
return getTypeOfSymbol(symbol); // intentionally doesn't use resolved symbol so type is cached as expected on the alias
1084010838
}
1084110839
else {
10842-
return links.resolvedType = getTypeReferenceType(node, resolvedSymbol); // getTypeReferenceType doesn't handle aliases - it must get the resolved symbol
10840+
return getTypeReferenceType(node, resolvedSymbol); // getTypeReferenceType doesn't handle aliases - it must get the resolved symbol
1084310841
}
1084410842
}
1084510843

@@ -14202,7 +14200,7 @@ namespace ts {
1420214200
if (isGenericMappedType(source)) {
1420314201
// A generic mapped type { [P in K]: T } is related to an index signature { [x: string]: U }
1420414202
// if T is related to U.
14205-
return (kind === IndexKind.String && isRelatedTo(getTemplateTypeFromMappedType(source), targetInfo.type, reportErrors)) as any as Ternary; // TODO: GH#18217
14203+
return kind === IndexKind.String ? isRelatedTo(getTemplateTypeFromMappedType(source), targetInfo.type, reportErrors) : Ternary.False;
1420614204
}
1420714205
if (isObjectTypeWithInferableIndex(source)) {
1420814206
let related = Ternary.True;

src/compiler/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ namespace ts {
11451145
// If we change our policy of rechecking failed lookups on each program create,
11461146
// we should adjust the value returned here.
11471147
function moduleNameResolvesToAmbientModuleInNonModifiedFile(moduleName: string): boolean {
1148-
const resolutionToFile = getResolvedModule(oldSourceFile!, moduleName);
1148+
const resolutionToFile = getResolvedModule(oldSourceFile, moduleName);
11491149
const resolvedFile = resolutionToFile && oldProgram!.getSourceFile(resolutionToFile.resolvedFileName);
11501150
if (resolutionToFile && resolvedFile) {
11511151
// In the old program, we resolved to an ambient module that was in the same

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3345,7 +3345,7 @@ namespace ts {
33453345
* This should be called in a loop climbing parents of the symbol, so we'll get `N`.
33463346
*/
33473347
/* @internal */ getAccessibleSymbolChain(symbol: Symbol, enclosingDeclaration: Node | undefined, meaning: SymbolFlags, useOnlyExternalAliasing: boolean): Symbol[] | undefined;
3348-
/* @internal */ getTypePredicateOfSignature(signature: Signature): TypePredicate;
3348+
/* @internal */ getTypePredicateOfSignature(signature: Signature): TypePredicate | undefined;
33493349
/**
33503350
* An external module with an 'export =' declaration resolves to the target of the 'export =' declaration,
33513351
* and an external module with no 'export =' declaration resolves to the module itself.

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ namespace ts {
220220
return node.end - node.pos;
221221
}
222222

223-
export function getResolvedModule(sourceFile: SourceFile, moduleNameText: string): ResolvedModuleFull | undefined {
223+
export function getResolvedModule(sourceFile: SourceFile | undefined, moduleNameText: string): ResolvedModuleFull | undefined {
224224
return sourceFile && sourceFile.resolvedModules && sourceFile.resolvedModules.get(moduleNameText);
225225
}
226226

0 commit comments

Comments
 (0)