diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 06b15c7718daf..07020343a0a4c 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -315,7 +315,7 @@ namespace ts { } function getDisplayName(node: Declaration): string { - return (node as NamedDeclaration).name ? declarationNameToString((node as NamedDeclaration).name) : unescapeLeadingUnderscores(getDeclarationName(node)); + return isNamedDeclaration(node) ? declarationNameToString(node.name) : unescapeLeadingUnderscores(getDeclarationName(node)); } /** @@ -383,8 +383,8 @@ namespace ts { symbolTable.set(name, symbol = createSymbol(SymbolFlags.None, name)); } else { - if ((node as NamedDeclaration).name) { - (node as NamedDeclaration).name.parent = node; + if (isNamedDeclaration(node)) { + node.name.parent = node; } // Report errors every position with duplicate declaration diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 63214d68e5d12..e0333dc42f5f1 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -4298,6 +4298,11 @@ namespace ts { return declaration.name || nameForNamelessJSDocTypedef(declaration); } + /** @internal */ + export function isNamedDeclaration(node: Node): node is NamedDeclaration & { name: DeclarationName } { + return !!(node as NamedDeclaration).name; // A 'name' property should always be a DeclarationName. + } + export function getNameOfDeclaration(declaration: Declaration | Expression): DeclarationName | undefined { if (!declaration) { return undefined;