Skip to content

Commit 63c8e56

Browse files
crisbetoatscott
authored andcommitted
fix(compiler-cli): incorrectly interpreting $any calls with a property read (#44657)
This was flagged during the code review of #44580. When generating a type check block, we were interpreting any call to `$any` as an `as any` cast, even if it's part of a `PropertyRead` (e.g. `foo.$any(1)`). This is handled correctly in other parts of the compiler, but it looks like it was missed in the type checker. PR Close #44657
1 parent 4de2b6d commit 63c8e56

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

packages/compiler-cli/src/ngtsc/typecheck/src/type_check_block.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,11 +1728,12 @@ class TcbExpressionTranslator {
17281728
return result;
17291729
} else if (
17301730
ast instanceof Call &&
1731-
(ast.receiver instanceof PropertyRead || ast.receiver instanceof SafePropertyRead) &&
1732-
!(ast.receiver.receiver instanceof ThisReceiver)) {
1731+
(ast.receiver instanceof PropertyRead || ast.receiver instanceof SafePropertyRead)) {
17331732
// Resolve the special `$any(expr)` syntax to insert a cast of the argument to type `any`.
17341733
// `$any(expr)` -> `expr as any`
1735-
if (ast.receiver.name === '$any' && ast.args.length === 1) {
1734+
if (ast.receiver.receiver instanceof ImplicitReceiver &&
1735+
!(ast.receiver.receiver instanceof ThisReceiver) && ast.receiver.name === '$any' &&
1736+
ast.args.length === 1) {
17361737
const expr = this.translate(ast.args[0]);
17371738
const exprAsAny =
17381739
ts.createAsExpression(expr, ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword));

packages/compiler-cli/src/ngtsc/typecheck/test/type_check_block_spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import {initMockFileSystem} from '../../file_system/testing';
1010
import {TypeCheckingConfig} from '../api';
11-
1211
import {ALL_ENABLED_CONFIG, tcb, TestDeclaration, TestDirective} from '../testing';
1312

1413

@@ -613,6 +612,12 @@ describe('type check blocks', () => {
613612
expect(block).toContain('((((ctx).$any))(((ctx).a)))');
614613
});
615614

615+
it('should handle $any accessed through a property read', () => {
616+
const TEMPLATE = `{{foo.$any(a)}}`;
617+
const block = tcb(TEMPLATE);
618+
expect(block).toContain('((((((ctx).foo)).$any))(((ctx).a)))');
619+
});
620+
616621
describe('experimental DOM checking via lib.dom.d.ts', () => {
617622
it('should translate unclaimed bindings to their property equivalent', () => {
618623
const TEMPLATE = `<label [for]="'test'"></label>`;

0 commit comments

Comments
 (0)