From 33630dd3eda4be52efcaa2bdd4a70e6547883a7e Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Fri, 13 Apr 2018 20:44:26 -0700 Subject: [PATCH] fix(ivy): workaround for tsickle bug (#23379) The issue is with tsickle type inference and the bug should be assigned to them. The offending code is: ``` function cacheMatchingDirectivesForNode( tNode: TNode, tView: TView, localRefs: string[] | null): void { const exportsMap = localRefs ? {'': -1} : null; // <<<<< ===== OFFENDING LINE const matches = tView.currentMatches = findDirectiveMatches(tNode); if (matches) { for (let i = 0; i < matches.length; i += 2) { const def = matches[i] as DirectiveDef; const valueIndex = i + 1; resolveDirective(def, valueIndex, matches, tView); saveNameToExportMap(matches[valueIndex] as number, def, exportsMap); } } if (exportsMap) cacheMatchingLocalNames(tNode, localRefs, exportsMap); } ``` because it generates invalid js closure code: ``` function cacheMatchingDirectivesForNode(tNode, tView, localRefs) { const /** @type {(null|{: number})} */ exportsMap = localRefs ? { '': -1 } : null; // <<<<< ===== OFFENDING LINE const /** @type {(null|!Array)} */ matches = tView.currentMatches = findDirectiveMatches(tNode); if (matches) { for (let /** @type {number} */ i = 0; i < matches.length; i += 2) { const /** @type {!tsickle_forward_declare_11.DirectiveDef} */ def = /** @type {!tsickle_forward_declare_11.DirectiveDef} */ (matches[i]); const /** @type {number} */ valueIndex = i + 1; resolveDirective(def, valueIndex, matches, tView); saveNameToExportMap(/** @type {number} */ (matches[valueIndex]), def, exportsMap); } } if (exportsMap) cacheMatchingLocalNames(tNode, localRefs, exportsMap); } ``` The workaround is to declare the type explicitly such as: ``` const exportsMap: ({[key:string]:number}|null) = localRefs ? {'': -1} : null; ``` which than generates valid closure code: ``` const /** @type {(null|!Object)} */ exportsMap = localRefs ? { '': -1 } : null; ``` PR Close #23379 --- packages/core/src/render3/instructions.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/core/src/render3/instructions.ts b/packages/core/src/render3/instructions.ts index a6eef84dfc..280a771686 100644 --- a/packages/core/src/render3/instructions.ts +++ b/packages/core/src/render3/instructions.ts @@ -574,7 +574,8 @@ function createDirectivesAndLocals( */ function cacheMatchingDirectivesForNode( tNode: TNode, tView: TView, localRefs: string[] | null): void { - const exportsMap = localRefs ? {'': -1} : null; + // Please make sure to have explicit type for `exportsMap`. Inferred type triggers bug in tsickle. + const exportsMap: ({[key: string]: number} | null) = localRefs ? {'': -1} : null; const matches = tView.currentMatches = findDirectiveMatches(tNode); if (matches) { for (let i = 0; i < matches.length; i += 2) {