From 10a1e1974b816ebb979dc10586b160ee07ad8356 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Fri, 14 Jun 2019 13:12:34 +0200 Subject: [PATCH] fix(platform-browser): debug element query predicates not compatible with strictFunctionTypes (#30993) Currently developers can use the `By` class to construct common `DebugElement` query predicates. e.g. `By.directive(MyDirective)`. The `directive()` and `all()` predicates are currently returning a predicate that works for `DebugElement` nodes. This return type is too strict since the predicate is not specific to `DebugElement` instances and can also apply to `DebugNode` instances. Meaning that developers are currently able to use the `directive()` predicate when using `queryAllNodes()`. This is a common practice but will break when the project is compiled with TypeScript's `--strictFunctionTypes` flag as the `DebugElement` predicate type is not assignable to predicates for `DebugNode`. In order to make these predicates usable with `--strictFuntionTypes` enabled, we adjust the predicate type to reflect what is actually needed for evaluation of the predicate. PR Close #30993 --- packages/platform-browser/src/dom/debug/by.ts | 12 ++++++------ .../platform-browser/platform-browser.d.ts | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/platform-browser/src/dom/debug/by.ts b/packages/platform-browser/src/dom/debug/by.ts index b8941f604c..2d7006b16f 100644 --- a/packages/platform-browser/src/dom/debug/by.ts +++ b/packages/platform-browser/src/dom/debug/by.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {DebugElement, Predicate, Type} from '@angular/core'; +import {DebugElement, DebugNode, Predicate, Type} from '@angular/core'; import {getDOM} from '../../dom/dom_adapter'; @@ -18,14 +18,14 @@ import {getDOM} from '../../dom/dom_adapter'; */ export class By { /** - * Match all elements. + * Match all nodes. * * @usageNotes * ### Example * * {@example platform-browser/dom/debug/ts/by/by.ts region='by_all'} */ - static all(): Predicate { return (debugElement) => true; } + static all(): Predicate { return () => true; } /** * Match elements by the given CSS selector. @@ -44,14 +44,14 @@ export class By { } /** - * Match elements that have the given directive present. + * Match nodes that have the given directive present. * * @usageNotes * ### Example * * {@example platform-browser/dom/debug/ts/by/by.ts region='by_directive'} */ - static directive(type: Type): Predicate { - return (debugElement) => debugElement.providerTokens !.indexOf(type) !== -1; + static directive(type: Type): Predicate { + return (debugNode) => debugNode.providerTokens !.indexOf(type) !== -1; } } diff --git a/tools/public_api_guard/platform-browser/platform-browser.d.ts b/tools/public_api_guard/platform-browser/platform-browser.d.ts index e6a3d02824..1d380def79 100644 --- a/tools/public_api_guard/platform-browser/platform-browser.d.ts +++ b/tools/public_api_guard/platform-browser/platform-browser.d.ts @@ -9,9 +9,9 @@ export declare class BrowserTransferStateModule { } export declare class By { - static all(): Predicate; + static all(): Predicate; static css(selector: string): Predicate; - static directive(type: Type): Predicate; + static directive(type: Type): Predicate; } export declare function disableDebugTools(): void;