Skip to content

Commit 2676786

Browse files
author
Andy
authored
Add 'isNamedDeclaration' helper to reduce casts (#22089)
* Add 'isNamedDeclaration' helper to reduce casts * Add assertion * Remove assertion
1 parent 88ba1ef commit 2676786

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/compiler/binder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ namespace ts {
315315
}
316316

317317
function getDisplayName(node: Declaration): string {
318-
return (node as NamedDeclaration).name ? declarationNameToString((node as NamedDeclaration).name) : unescapeLeadingUnderscores(getDeclarationName(node));
318+
return isNamedDeclaration(node) ? declarationNameToString(node.name) : unescapeLeadingUnderscores(getDeclarationName(node));
319319
}
320320

321321
/**
@@ -383,8 +383,8 @@ namespace ts {
383383
symbolTable.set(name, symbol = createSymbol(SymbolFlags.None, name));
384384
}
385385
else {
386-
if ((node as NamedDeclaration).name) {
387-
(node as NamedDeclaration).name.parent = node;
386+
if (isNamedDeclaration(node)) {
387+
node.name.parent = node;
388388
}
389389

390390
// Report errors every position with duplicate declaration

src/compiler/utilities.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4250,6 +4250,11 @@ namespace ts {
42504250
return declaration.name || nameForNamelessJSDocTypedef(declaration);
42514251
}
42524252

4253+
/** @internal */
4254+
export function isNamedDeclaration(node: Node): node is NamedDeclaration & { name: DeclarationName } {
4255+
return !!(node as NamedDeclaration).name; // A 'name' property should always be a DeclarationName.
4256+
}
4257+
42534258
export function getNameOfDeclaration(declaration: Declaration | Expression): DeclarationName | undefined {
42544259
if (!declaration) {
42554260
return undefined;

0 commit comments

Comments
 (0)