Skip to content

Commit 0bc3468

Browse files
small refactor to cut back on type assertions (microsoft#32920)
1 parent 6cd8bcd commit 0bc3468

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
@@ -386,11 +386,11 @@ namespace ts {
386386
typeToTypeNode: nodeBuilder.typeToTypeNode,
387387
indexInfoToIndexSignatureDeclaration: nodeBuilder.indexInfoToIndexSignatureDeclaration,
388388
signatureToSignatureDeclaration: nodeBuilder.signatureToSignatureDeclaration,
389-
symbolToEntityName: nodeBuilder.symbolToEntityName as (symbol: Symbol, meaning: SymbolFlags, enclosingDeclaration?: Node, flags?: NodeBuilderFlags) => EntityName, // TODO: GH#18217
390-
symbolToExpression: nodeBuilder.symbolToExpression as (symbol: Symbol, meaning: SymbolFlags, enclosingDeclaration?: Node, flags?: NodeBuilderFlags) => Expression, // TODO: GH#18217
389+
symbolToEntityName: nodeBuilder.symbolToEntityName,
390+
symbolToExpression: nodeBuilder.symbolToExpression,
391391
symbolToTypeParameterDeclarations: nodeBuilder.symbolToTypeParameterDeclarations,
392-
symbolToParameterDeclaration: nodeBuilder.symbolToParameterDeclaration as (symbol: Symbol, enclosingDeclaration?: Node, flags?: NodeBuilderFlags) => ParameterDeclaration, // TODO: GH#18217
393-
typeParameterToDeclaration: nodeBuilder.typeParameterToDeclaration as (parameter: TypeParameter, enclosingDeclaration?: Node, flags?: NodeBuilderFlags) => TypeParameterDeclaration, // TODO: GH#18217
392+
symbolToParameterDeclaration: nodeBuilder.symbolToParameterDeclaration,
393+
typeParameterToDeclaration: nodeBuilder.typeParameterToDeclaration,
394394
getSymbolsInScope: (location, meaning) => {
395395
location = getParseTreeNode(location);
396396
return location ? getSymbolsInScope(location, meaning) : [];
@@ -559,7 +559,7 @@ namespace ts {
559559
},
560560
getJsxNamespace: n => unescapeLeadingUnderscores(getJsxNamespace(n)),
561561
getAccessibleSymbolChain,
562-
getTypePredicateOfSignature: getTypePredicateOfSignature as (signature: Signature) => TypePredicate, // TODO: GH#18217
562+
getTypePredicateOfSignature,
563563
resolveExternalModuleSymbol,
564564
tryGetThisTypeAt: (node, includeGlobalThis) => {
565565
node = getParseTreeNode(node);
@@ -886,7 +886,7 @@ namespace ts {
886886
}
887887
const jsxPragma = file.pragmas.get("jsx");
888888
if (jsxPragma) {
889-
const chosenpragma: any = isArray(jsxPragma) ? jsxPragma[0] : jsxPragma; // TODO: GH#18217
889+
const chosenpragma = isArray(jsxPragma) ? jsxPragma[0] : jsxPragma;
890890
file.localJsxFactory = parseIsolatedEntityName(chosenpragma.arguments.factory, languageVersion);
891891
if (file.localJsxFactory) {
892892
return file.localJsxNamespace = getFirstIdentifier(file.localJsxFactory).escapedText;
@@ -2567,10 +2567,6 @@ namespace ts {
25672567
}
25682568

25692569
function resolveExternalModule(location: Node, moduleReference: string, moduleNotFoundError: DiagnosticMessage | undefined, errorNode: Node, isForAugmentation = false): Symbol | undefined {
2570-
if (moduleReference === undefined) {
2571-
return;
2572-
}
2573-
25742570
if (startsWith(moduleReference, "@types/")) {
25752571
const diag = Diagnostics.Cannot_import_type_declaration_files_Consider_importing_0_instead_of_1;
25762572
const withoutAtTypePrefix = removePrefix(moduleReference, "@types/");
@@ -3268,12 +3264,12 @@ namespace ts {
32683264
return access.accessibility === SymbolAccessibility.Accessible;
32693265
}
32703266

3271-
function isValueSymbolAccessible(typeSymbol: Symbol, enclosingDeclaration: Node): boolean {
3267+
function isValueSymbolAccessible(typeSymbol: Symbol, enclosingDeclaration: Node | undefined): boolean {
32723268
const access = isSymbolAccessible(typeSymbol, enclosingDeclaration, SymbolFlags.Value, /*shouldComputeAliasesToMakeVisible*/ false);
32733269
return access.accessibility === SymbolAccessibility.Accessible;
32743270
}
32753271

3276-
function isAnySymbolAccessible(symbols: Symbol[] | undefined, enclosingDeclaration: Node, initialSymbol: Symbol, meaning: SymbolFlags, shouldComputeAliasesToMakeVisible: boolean): SymbolAccessibilityResult | undefined {
3272+
function isAnySymbolAccessible(symbols: Symbol[] | undefined, enclosingDeclaration: Node | undefined, initialSymbol: Symbol, meaning: SymbolFlags, shouldComputeAliasesToMakeVisible: boolean): SymbolAccessibilityResult | undefined {
32773273
if (!length(symbols)) return;
32783274

32793275
let hadAccessibleChain: Symbol | undefined;
@@ -3547,7 +3543,7 @@ namespace ts {
35473543
typeToTypeNode: (type: Type, enclosingDeclaration?: Node, flags?: NodeBuilderFlags, tracker?: SymbolTracker) =>
35483544
withContext(enclosingDeclaration, flags, tracker, context => typeToTypeNodeHelper(type, context)),
35493545
indexInfoToIndexSignatureDeclaration: (indexInfo: IndexInfo, kind: IndexKind, enclosingDeclaration?: Node, flags?: NodeBuilderFlags, tracker?: SymbolTracker) =>
3550-
withContext(enclosingDeclaration, flags, tracker, context => indexInfoToIndexSignatureDeclarationHelper(indexInfo, kind, context))!, // TODO: GH#18217
3546+
withContext(enclosingDeclaration, flags, tracker, context => indexInfoToIndexSignatureDeclarationHelper(indexInfo, kind, context)),
35513547
signatureToSignatureDeclaration: (signature: Signature, kind: SyntaxKind, enclosingDeclaration?: Node, flags?: NodeBuilderFlags, tracker?: SymbolTracker) =>
35523548
withContext(enclosingDeclaration, flags, tracker, context => signatureToSignatureDeclarationHelper(signature, kind, context)),
35533549
symbolToEntityName: (symbol: Symbol, meaning: SymbolFlags, enclosingDeclaration?: Node, flags?: NodeBuilderFlags, tracker?: SymbolTracker) =>
@@ -3656,7 +3652,7 @@ namespace ts {
36563652
}
36573653
if (type.flags & TypeFlags.UniqueESSymbol) {
36583654
if (!(context.flags & NodeBuilderFlags.AllowUniqueESSymbolType)) {
3659-
if (isValueSymbolAccessible(type.symbol, context.enclosingDeclaration!)) {
3655+
if (isValueSymbolAccessible(type.symbol, context.enclosingDeclaration)) {
36603656
context.approximateLength += 6;
36613657
return symbolToTypeNode(type.symbol, context, SymbolFlags.Value);
36623658
}
@@ -3867,7 +3863,7 @@ namespace ts {
38673863
if (isStaticMethodSymbol || isNonLocalFunctionSymbol) {
38683864
// typeof is allowed only for static/non local functions
38693865
return (!!(context.flags & NodeBuilderFlags.UseTypeOfFunction) || (context.visitedTypes && context.visitedTypes.has(typeId))) && // it is type of the symbol uses itself recursively
3870-
(!(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
3866+
(!(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
38713867
}
38723868
}
38733869
}
@@ -3944,7 +3940,7 @@ namespace ts {
39443940
else if (context.flags & NodeBuilderFlags.WriteClassExpressionAsTypeLiteral &&
39453941
type.symbol.valueDeclaration &&
39463942
isClassLike(type.symbol.valueDeclaration) &&
3947-
!isValueSymbolAccessible(type.symbol, context.enclosingDeclaration!)
3943+
!isValueSymbolAccessible(type.symbol, context.enclosingDeclaration)
39483944
) {
39493945
return createAnonymousTypeNode(type);
39503946
}
@@ -7160,7 +7156,7 @@ namespace ts {
71607156
const baseConstructorType = getBaseConstructorTypeOfClass(classType);
71617157
const baseSignatures = getSignaturesOfType(baseConstructorType, SignatureKind.Construct);
71627158
if (baseSignatures.length === 0) {
7163-
return [createSignature(undefined, classType.localTypeParameters, undefined, emptyArray, classType, /*resolvedTypePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false)]; // TODO: GH#18217
7159+
return [createSignature(undefined, classType.localTypeParameters, undefined, emptyArray, classType, /*resolvedTypePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false)];
71647160
}
71657161
const baseTypeNode = getBaseTypeNodeOfClass(classType)!;
71667162
const isJavaScript = isInJSFile(baseTypeNode);
@@ -10229,9 +10225,11 @@ namespace ts {
1022910225
case SyntaxKind.ReadonlyKeyword:
1023010226
links.resolvedType = getTypeFromTypeNode(node.type);
1023110227
break;
10228+
default:
10229+
throw Debug.assertNever(node.operator);
1023210230
}
1023310231
}
10234-
return links.resolvedType!; // TODO: GH#18217
10232+
return links.resolvedType;
1023510233
}
1023610234

1023710235
function createIndexedAccessType(objectType: Type, indexType: Type) {
@@ -10816,11 +10814,11 @@ namespace ts {
1081610814
getNodeLinks(current.parent).resolvedSymbol = next;
1081710815
currentNamespace = next;
1081810816
}
10819-
resolveImportSymbolType(node, links, currentNamespace, targetMeaning);
10817+
links.resolvedType = resolveImportSymbolType(node, links, currentNamespace, targetMeaning);
1082010818
}
1082110819
else {
1082210820
if (moduleSymbol.flags & targetMeaning) {
10823-
resolveImportSymbolType(node, links, moduleSymbol, targetMeaning);
10821+
links.resolvedType = resolveImportSymbolType(node, links, moduleSymbol, targetMeaning);
1082410822
}
1082510823
else {
1082610824
const errorMessage = targetMeaning === SymbolFlags.Value
@@ -10834,17 +10832,17 @@ namespace ts {
1083410832
}
1083510833
}
1083610834
}
10837-
return links.resolvedType!; // TODO: GH#18217
10835+
return links.resolvedType;
1083810836
}
1083910837

1084010838
function resolveImportSymbolType(node: ImportTypeNode, links: NodeLinks, symbol: Symbol, meaning: SymbolFlags) {
1084110839
const resolvedSymbol = resolveSymbol(symbol);
1084210840
links.resolvedSymbol = resolvedSymbol;
1084310841
if (meaning === SymbolFlags.Value) {
10844-
return links.resolvedType = getTypeOfSymbol(symbol); // intentionally doesn't use resolved symbol so type is cached as expected on the alias
10842+
return getTypeOfSymbol(symbol); // intentionally doesn't use resolved symbol so type is cached as expected on the alias
1084510843
}
1084610844
else {
10847-
return links.resolvedType = getTypeReferenceType(node, resolvedSymbol); // getTypeReferenceType doesn't handle aliases - it must get the resolved symbol
10845+
return getTypeReferenceType(node, resolvedSymbol); // getTypeReferenceType doesn't handle aliases - it must get the resolved symbol
1084810846
}
1084910847
}
1085010848

@@ -14207,7 +14205,7 @@ namespace ts {
1420714205
if (isGenericMappedType(source)) {
1420814206
// A generic mapped type { [P in K]: T } is related to an index signature { [x: string]: U }
1420914207
// if T is related to U.
14210-
return (kind === IndexKind.String && isRelatedTo(getTemplateTypeFromMappedType(source), targetInfo.type, reportErrors)) as any as Ternary; // TODO: GH#18217
14208+
return kind === IndexKind.String ? isRelatedTo(getTemplateTypeFromMappedType(source), targetInfo.type, reportErrors) : Ternary.False;
1421114209
}
1421214210
if (isObjectTypeWithInferableIndex(source)) {
1421314211
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)