Skip to content

Commit

Permalink
fix(core): allow readonly arrays for standalone imports (#47851)
Browse files Browse the repository at this point in the history
Standalone components should support readonly arrays in the
`@Component.imports`.

Fixes #47643

PR Close #47851
  • Loading branch information
pkozlowski-opensource committed Oct 27, 2022
1 parent f24da6b commit 96c0e42
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion goldens/public-api/core/index.md
Expand Up @@ -188,7 +188,7 @@ export interface Component extends Directive {
encapsulation?: ViewEncapsulation;
// @deprecated
entryComponents?: Array<Type<any> | any[]>;
imports?: (Type<any> | any[])[];
imports?: (Type<any> | ReadonlyArray<any>)[];
interpolation?: [string, string];
moduleId?: string;
preserveWhitespaces?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/metadata/directives.ts
Expand Up @@ -629,7 +629,7 @@ export interface Component extends Directive {
* More information about standalone components, directives, and pipes can be found in [this
* guide](guide/standalone-components).
*/
imports?: (Type<any>|any[])[];
imports?: (Type<any>|ReadonlyArray<any>)[];

/**
* The set of schemas that declare elements to be allowed in a standalone component. Elements and
Expand Down
30 changes: 29 additions & 1 deletion packages/core/test/acceptance/standalone_spec.ts
Expand Up @@ -152,7 +152,7 @@ describe('standalone components, directives, and pipes', () => {
});


it('should render a standalone component with dependenices and ambient providers', () => {
it('should render a standalone component with dependencies and ambient providers', () => {
@Component({
standalone: true,
template: 'Inner',
Expand Down Expand Up @@ -453,6 +453,34 @@ describe('standalone components, directives, and pipes', () => {
expect(fixture.nativeElement.innerHTML).toBe('<div red="true">blue</div>');
});

it('should support readonly arrays in @Component.imports', () => {
@Directive({selector: '[red]', standalone: true, host: {'[attr.red]': 'true'}})
class RedIdDirective {
}

@Pipe({name: 'blue', pure: true, standalone: true})
class BluePipe implements PipeTransform {
transform() {
return 'blue';
}
}

const DirAndPipe = [RedIdDirective, BluePipe] as const;

@Component({
selector: 'standalone',
standalone: true,
template: `<div red>{{'' | blue}}</div>`,
imports: [DirAndPipe],
})
class TestComponent {
}

const fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
expect(fixture.nativeElement.innerHTML).toBe('<div red="true">blue</div>');
});

it('should deduplicate declarations', () => {
@Component({selector: 'test-red', standalone: true, template: 'red(<ng-content></ng-content>)'})
class RedComponent {
Expand Down

0 comments on commit 96c0e42

Please sign in to comment.