Skip to content

Commit 0656e41

Browse files
committed
small refactor to cut back on type assertions
1 parent 454b428 commit 0656e41

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

src/compiler/checker.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ namespace ts {
156156
typeToTypeNode: nodeBuilder.typeToTypeNode,
157157
indexInfoToIndexSignatureDeclaration: nodeBuilder.indexInfoToIndexSignatureDeclaration,
158158
signatureToSignatureDeclaration: nodeBuilder.signatureToSignatureDeclaration,
159-
symbolToEntityName: nodeBuilder.symbolToEntityName as (symbol: Symbol, meaning: SymbolFlags, enclosingDeclaration?: Node, flags?: NodeBuilderFlags) => EntityName, // TODO: GH#18217
160-
symbolToExpression: nodeBuilder.symbolToExpression as (symbol: Symbol, meaning: SymbolFlags, enclosingDeclaration?: Node, flags?: NodeBuilderFlags) => Expression, // TODO: GH#18217
159+
symbolToEntityName: nodeBuilder.symbolToEntityName,
160+
symbolToExpression: nodeBuilder.symbolToExpression,
161161
symbolToTypeParameterDeclarations: nodeBuilder.symbolToTypeParameterDeclarations,
162-
symbolToParameterDeclaration: nodeBuilder.symbolToParameterDeclaration as (symbol: Symbol, enclosingDeclaration?: Node, flags?: NodeBuilderFlags) => ParameterDeclaration, // TODO: GH#18217
163-
typeParameterToDeclaration: nodeBuilder.typeParameterToDeclaration as (parameter: TypeParameter, enclosingDeclaration?: Node, flags?: NodeBuilderFlags) => TypeParameterDeclaration, // TODO: GH#18217
162+
symbolToParameterDeclaration: nodeBuilder.symbolToParameterDeclaration,
163+
typeParameterToDeclaration: nodeBuilder.typeParameterToDeclaration,
164164
getSymbolsInScope: (location, meaning) => {
165165
location = getParseTreeNode(location);
166166
return location ? getSymbolsInScope(location, meaning) : [];
@@ -325,7 +325,7 @@ namespace ts {
325325
},
326326
getJsxNamespace: n => unescapeLeadingUnderscores(getJsxNamespace(n)),
327327
getAccessibleSymbolChain,
328-
getTypePredicateOfSignature: getTypePredicateOfSignature as (signature: Signature) => TypePredicate, // TODO: GH#18217
328+
getTypePredicateOfSignature,
329329
resolveExternalModuleSymbol,
330330
tryGetThisTypeAt: (node, includeGlobalThis) => {
331331
node = getParseTreeNode(node);
@@ -770,7 +770,7 @@ namespace ts {
770770
}
771771
const jsxPragma = file.pragmas.get("jsx");
772772
if (jsxPragma) {
773-
const chosenpragma: any = isArray(jsxPragma) ? jsxPragma[0] : jsxPragma; // TODO: GH#18217
773+
const chosenpragma = isArray(jsxPragma) ? jsxPragma[0] : jsxPragma;
774774
file.localJsxFactory = parseIsolatedEntityName(chosenpragma.arguments.factory, languageVersion);
775775
if (file.localJsxFactory) {
776776
return file.localJsxNamespace = getFirstIdentifier(file.localJsxFactory).escapedText;
@@ -2416,10 +2416,6 @@ namespace ts {
24162416
}
24172417

24182418
function resolveExternalModule(location: Node, moduleReference: string, moduleNotFoundError: DiagnosticMessage | undefined, errorNode: Node, isForAugmentation = false): Symbol | undefined {
2419-
if (moduleReference === undefined) {
2420-
return;
2421-
}
2422-
24232419
if (startsWith(moduleReference, "@types/")) {
24242420
const diag = Diagnostics.Cannot_import_type_declaration_files_Consider_importing_0_instead_of_1;
24252421
const withoutAtTypePrefix = removePrefix(moduleReference, "@types/");
@@ -3109,12 +3105,12 @@ namespace ts {
31093105
return access.accessibility === SymbolAccessibility.Accessible;
31103106
}
31113107

3112-
function isValueSymbolAccessible(typeSymbol: Symbol, enclosingDeclaration: Node): boolean {
3108+
function isValueSymbolAccessible(typeSymbol: Symbol, enclosingDeclaration: Node | undefined): boolean {
31133109
const access = isSymbolAccessible(typeSymbol, enclosingDeclaration, SymbolFlags.Value, /*shouldComputeAliasesToMakeVisible*/ false);
31143110
return access.accessibility === SymbolAccessibility.Accessible;
31153111
}
31163112

3117-
function isAnySymbolAccessible(symbols: Symbol[] | undefined, enclosingDeclaration: Node, initialSymbol: Symbol, meaning: SymbolFlags, shouldComputeAliasesToMakeVisible: boolean): SymbolAccessibilityResult | undefined {
3113+
function isAnySymbolAccessible(symbols: Symbol[] | undefined, enclosingDeclaration: Node | undefined, initialSymbol: Symbol, meaning: SymbolFlags, shouldComputeAliasesToMakeVisible: boolean): SymbolAccessibilityResult | undefined {
31183114
if (!length(symbols)) return;
31193115

31203116
let hadAccessibleChain: Symbol | undefined;
@@ -3388,7 +3384,7 @@ namespace ts {
33883384
typeToTypeNode: (type: Type, enclosingDeclaration?: Node, flags?: NodeBuilderFlags, tracker?: SymbolTracker) =>
33893385
withContext(enclosingDeclaration, flags, tracker, context => typeToTypeNodeHelper(type, context)),
33903386
indexInfoToIndexSignatureDeclaration: (indexInfo: IndexInfo, kind: IndexKind, enclosingDeclaration?: Node, flags?: NodeBuilderFlags, tracker?: SymbolTracker) =>
3391-
withContext(enclosingDeclaration, flags, tracker, context => indexInfoToIndexSignatureDeclarationHelper(indexInfo, kind, context))!, // TODO: GH#18217
3387+
withContext(enclosingDeclaration, flags, tracker, context => indexInfoToIndexSignatureDeclarationHelper(indexInfo, kind, context)),
33923388
signatureToSignatureDeclaration: (signature: Signature, kind: SyntaxKind, enclosingDeclaration?: Node, flags?: NodeBuilderFlags, tracker?: SymbolTracker) =>
33933389
withContext(enclosingDeclaration, flags, tracker, context => signatureToSignatureDeclarationHelper(signature, kind, context)),
33943390
symbolToEntityName: (symbol: Symbol, meaning: SymbolFlags, enclosingDeclaration?: Node, flags?: NodeBuilderFlags, tracker?: SymbolTracker) =>
@@ -3497,7 +3493,7 @@ namespace ts {
34973493
}
34983494
if (type.flags & TypeFlags.UniqueESSymbol) {
34993495
if (!(context.flags & NodeBuilderFlags.AllowUniqueESSymbolType)) {
3500-
if (isValueSymbolAccessible(type.symbol, context.enclosingDeclaration!)) {
3496+
if (isValueSymbolAccessible(type.symbol, context.enclosingDeclaration)) {
35013497
context.approximateLength += 6;
35023498
return symbolToTypeNode(type.symbol, context, SymbolFlags.Value);
35033499
}
@@ -3712,7 +3708,7 @@ namespace ts {
37123708
if (isStaticMethodSymbol || isNonLocalFunctionSymbol) {
37133709
// typeof is allowed only for static/non local functions
37143710
return (!!(context.flags & NodeBuilderFlags.UseTypeOfFunction) || (context.visitedTypes && context.visitedTypes.has(typeId))) && // it is type of the symbol uses itself recursively
3715-
(!(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
3711+
(!(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
37163712
}
37173713
}
37183714
}
@@ -3789,7 +3785,7 @@ namespace ts {
37893785
else if (context.flags & NodeBuilderFlags.WriteClassExpressionAsTypeLiteral &&
37903786
type.symbol.valueDeclaration &&
37913787
isClassLike(type.symbol.valueDeclaration) &&
3792-
!isValueSymbolAccessible(type.symbol, context.enclosingDeclaration!)
3788+
!isValueSymbolAccessible(type.symbol, context.enclosingDeclaration)
37933789
) {
37943790
return createAnonymousTypeNode(type);
37953791
}
@@ -6941,7 +6937,7 @@ namespace ts {
69416937
const baseConstructorType = getBaseConstructorTypeOfClass(classType);
69426938
const baseSignatures = getSignaturesOfType(baseConstructorType, SignatureKind.Construct);
69436939
if (baseSignatures.length === 0) {
6944-
return [createSignature(undefined, classType.localTypeParameters, undefined, emptyArray, classType, /*resolvedTypePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false)]; // TODO: GH#18217
6940+
return [createSignature(undefined, classType.localTypeParameters, undefined, emptyArray, classType, /*resolvedTypePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false)];
69456941
}
69466942
const baseTypeNode = getBaseTypeNodeOfClass(classType)!;
69476943
const isJavaScript = isInJSFile(baseTypeNode);
@@ -9980,9 +9976,11 @@ namespace ts {
99809976
case SyntaxKind.ReadonlyKeyword:
99819977
links.resolvedType = getTypeFromTypeNode(node.type);
99829978
break;
9979+
default:
9980+
throw Debug.assertNever(node.operator);
99839981
}
99849982
}
9985-
return links.resolvedType!; // TODO: GH#18217
9983+
return links.resolvedType;
99869984
}
99879985

99889986
function createIndexedAccessType(objectType: Type, indexType: Type) {
@@ -10538,11 +10536,11 @@ namespace ts {
1053810536
getNodeLinks(current.parent).resolvedSymbol = next;
1053910537
currentNamespace = next;
1054010538
}
10541-
resolveImportSymbolType(node, links, currentNamespace, targetMeaning);
10539+
links.resolvedType = resolveImportSymbolType(node, links, currentNamespace, targetMeaning);
1054210540
}
1054310541
else {
1054410542
if (moduleSymbol.flags & targetMeaning) {
10545-
resolveImportSymbolType(node, links, moduleSymbol, targetMeaning);
10543+
links.resolvedType = resolveImportSymbolType(node, links, moduleSymbol, targetMeaning);
1054610544
}
1054710545
else {
1054810546
const errorMessage = targetMeaning === SymbolFlags.Value
@@ -10556,17 +10554,17 @@ namespace ts {
1055610554
}
1055710555
}
1055810556
}
10559-
return links.resolvedType!; // TODO: GH#18217
10557+
return links.resolvedType;
1056010558
}
1056110559

1056210560
function resolveImportSymbolType(node: ImportTypeNode, links: NodeLinks, symbol: Symbol, meaning: SymbolFlags) {
1056310561
const resolvedSymbol = resolveSymbol(symbol);
1056410562
links.resolvedSymbol = resolvedSymbol;
1056510563
if (meaning === SymbolFlags.Value) {
10566-
return links.resolvedType = getTypeOfSymbol(symbol); // intentionally doesn't use resolved symbol so type is cached as expected on the alias
10564+
return getTypeOfSymbol(symbol); // intentionally doesn't use resolved symbol so type is cached as expected on the alias
1056710565
}
1056810566
else {
10569-
return links.resolvedType = getTypeReferenceType(node, resolvedSymbol); // getTypeReferenceType doesn't handle aliases - it must get the resolved symbol
10567+
return getTypeReferenceType(node, resolvedSymbol); // getTypeReferenceType doesn't handle aliases - it must get the resolved symbol
1057010568
}
1057110569
}
1057210570

@@ -12297,7 +12295,7 @@ namespace ts {
1229712295
if (errorOutputContainer) {
1229812296
errorOutputContainer.error = diag;
1229912297
}
12300-
diagnostics.add(diag); // TODO: GH#18217
12298+
diagnostics.add(diag);
1230112299
}
1230212300
return result !== Ternary.False;
1230312301

@@ -13585,7 +13583,7 @@ namespace ts {
1358513583
if (isGenericMappedType(source)) {
1358613584
// A generic mapped type { [P in K]: T } is related to an index signature { [x: string]: U }
1358713585
// if T is related to U.
13588-
return (kind === IndexKind.String && isRelatedTo(getTemplateTypeFromMappedType(source), targetInfo.type, reportErrors)) as any as Ternary; // TODO: GH#18217
13586+
return kind === IndexKind.String ? isRelatedTo(getTemplateTypeFromMappedType(source), targetInfo.type, reportErrors) : Ternary.False;
1358913587
}
1359013588
if (isObjectTypeWithInferableIndex(source)) {
1359113589
let related = Ternary.True;

src/compiler/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ namespace ts {
11341134
// If we change our policy of rechecking failed lookups on each program create,
11351135
// we should adjust the value returned here.
11361136
function moduleNameResolvesToAmbientModuleInNonModifiedFile(moduleName: string): boolean {
1137-
const resolutionToFile = getResolvedModule(oldSourceFile!, moduleName);
1137+
const resolutionToFile = getResolvedModule(oldSourceFile, moduleName);
11381138
const resolvedFile = resolutionToFile && oldProgram!.getSourceFile(resolutionToFile.resolvedFileName);
11391139
if (resolutionToFile && resolvedFile) {
11401140
// 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
@@ -3280,7 +3280,7 @@ namespace ts {
32803280
* This should be called in a loop climbing parents of the symbol, so we'll get `N`.
32813281
*/
32823282
/* @internal */ getAccessibleSymbolChain(symbol: Symbol, enclosingDeclaration: Node | undefined, meaning: SymbolFlags, useOnlyExternalAliasing: boolean): Symbol[] | undefined;
3283-
/* @internal */ getTypePredicateOfSignature(signature: Signature): TypePredicate;
3283+
/* @internal */ getTypePredicateOfSignature(signature: Signature): TypePredicate | undefined;
32843284
/**
32853285
* An external module with an 'export =' declaration resolves to the target of the 'export =' declaration,
32863286
* 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)