Skip to content

Commit 10a1e19

Browse files
devversionmhevery
authored andcommittedJul 18, 2019
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
1 parent 647d7bd commit 10a1e19

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed
 

‎packages/platform-browser/src/dom/debug/by.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {DebugElement, Predicate, Type} from '@angular/core';
9+
import {DebugElement, DebugNode, Predicate, Type} from '@angular/core';
1010
import {getDOM} from '../../dom/dom_adapter';
1111

1212

@@ -18,14 +18,14 @@ import {getDOM} from '../../dom/dom_adapter';
1818
*/
1919
export class By {
2020
/**
21-
* Match all elements.
21+
* Match all nodes.
2222
*
2323
* @usageNotes
2424
* ### Example
2525
*
2626
* {@example platform-browser/dom/debug/ts/by/by.ts region='by_all'}
2727
*/
28-
static all(): Predicate<DebugElement> { return (debugElement) => true; }
28+
static all(): Predicate<DebugNode> { return () => true; }
2929

3030
/**
3131
* Match elements by the given CSS selector.
@@ -44,14 +44,14 @@ export class By {
4444
}
4545

4646
/**
47-
* Match elements that have the given directive present.
47+
* Match nodes that have the given directive present.
4848
*
4949
* @usageNotes
5050
* ### Example
5151
*
5252
* {@example platform-browser/dom/debug/ts/by/by.ts region='by_directive'}
5353
*/
54-
static directive(type: Type<any>): Predicate<DebugElement> {
55-
return (debugElement) => debugElement.providerTokens !.indexOf(type) !== -1;
54+
static directive(type: Type<any>): Predicate<DebugNode> {
55+
return (debugNode) => debugNode.providerTokens !.indexOf(type) !== -1;
5656
}
5757
}

‎tools/public_api_guard/platform-browser/platform-browser.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ export declare class BrowserTransferStateModule {
99
}
1010

1111
export declare class By {
12-
static all(): Predicate<DebugElement>;
12+
static all(): Predicate<DebugNode>;
1313
static css(selector: string): Predicate<DebugElement>;
14-
static directive(type: Type<any>): Predicate<DebugElement>;
14+
static directive(type: Type<any>): Predicate<DebugNode>;
1515
}
1616

1717
export declare function disableDebugTools(): void;

0 commit comments

Comments
 (0)
Please sign in to comment.