Skip to content

Commit 7a0f8ac

Browse files
AndrewKushnirmatsko
authored andcommitted
fix(ivy): generate explicit type annotation for NgModuleFactory calls in ngfactories (angular#30708)
Prior to this commit there were no explicit types setup for NgModuleFactory calls in ngfactories, so TypeScript inferred the type based on a given call. In some cases (when generic types were used for Components/Directives) that turned out to be problematic, so we add explicit typing for NgModuleFactory calls. PR Close angular#30708
1 parent 5e0f982 commit 7a0f8ac

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

packages/compiler-cli/src/ngtsc/shims/src/factory_generator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ export class FactoryGenerator implements ShimGenerator {
7373
// This will encompass a lot of symbols which don't need factories, but that's okay
7474
// because it won't miss any that do.
7575
const varLines = symbolNames.map(
76-
name => `export const ${name}NgFactory = new i0.ɵNgModuleFactory(${name});`);
76+
name =>
77+
`export const ${name}NgFactory: i0.ɵNgModuleFactory<any> = new i0.ɵNgModuleFactory(${name});`);
7778
sourceText += [
7879
// This might be incorrect if the current package being compiled is Angular core, but it's
7980
// okay to leave in at type checking time. TypeScript can handle this reference via its path

packages/compiler-cli/test/ngtsc/ngtsc_spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,22 @@ describe('ngtsc behavioral tests', () => {
21492149
expect(emptyFactory).toContain(`export var \u0275NonEmptyModule = true;`);
21502150
});
21512151

2152+
it('should generate correct type annotation for NgModuleFactory calls in ngfactories', () => {
2153+
env.tsconfig({'allowEmptyCodegenFiles': true});
2154+
env.write('test.ts', `
2155+
import {Component} from '@angular/core';
2156+
@Component({
2157+
selector: 'test',
2158+
template: '...',
2159+
})
2160+
export class TestCmp {}
2161+
`);
2162+
env.driveMain();
2163+
2164+
const ngfactoryContents = env.getContents('test.ngfactory.d.ts');
2165+
expect(ngfactoryContents).toContain(`i0.ɵNgModuleFactory<any>`);
2166+
});
2167+
21522168
it('should copy a top-level comment into a factory stub', () => {
21532169
env.tsconfig({'allowEmptyCodegenFiles': true});
21542170

0 commit comments

Comments
 (0)