From b64a276d4b34c23a8d882e7bca5f306d2b6775c1 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Sat, 14 Apr 2018 09:18:38 -0700 Subject: [PATCH] refactor(ivy): make return value of define(Component|Directive|Pipe|Injector|Injectable) private (#23371) (#23383) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ivy definition looks something like this: ``` class MyService { static ngInjectableDef = defineInjectable({ … }); } ``` Here the argument to `defineInjectable` is well known public contract which needs to be honored in backward compatible way between versions. The type of the return value of `defineInjectable` on the other hand is private and can change shape drastically between versions without effecting backwards compatibility of libraries publish to NPM. To our users it is effectively an opaque token. For this reson why declare the return value of `defineInjectable` as `never`. PR Close #23383 --- .../differs/iterable_differs.ts | 4 +- packages/core/src/di.ts | 2 +- packages/core/src/di/defs.ts | 42 ++++++++++--------- packages/core/src/di/injection_token.ts | 6 +-- packages/core/src/di/r3_injector.ts | 2 +- packages/core/src/render3/definition.ts | 12 +++--- .../core/src/render3/interfaces/definition.ts | 6 +-- .../core/test/bundling/injection/usage.ts | 2 +- .../back_patch_types_specs.ts | 2 +- .../component_directives_spec.ts | 39 ++++++++++------- .../compiler_canonical/elements_spec.ts | 5 ++- .../compiler_canonical/injection_spec.ts | 2 +- .../compiler_canonical/life_cycle_spec.ts | 4 +- .../compiler_canonical/patch_types_spec.ts | 2 +- .../render3/compiler_canonical/pipes_spec.ts | 9 ++-- .../render3/compiler_canonical/query_spec.ts | 7 +++- .../compiler_canonical/small_app_spec.ts | 4 +- .../template_variables_spec.ts | 5 ++- packages/core/test/render3/component_spec.ts | 5 ++- packages/core/test/render3/define_spec.ts | 7 ++-- tools/public_api_guard/core/core.d.ts | 34 ++++----------- 21 files changed, 105 insertions(+), 96 deletions(-) diff --git a/packages/core/src/change_detection/differs/iterable_differs.ts b/packages/core/src/change_detection/differs/iterable_differs.ts index 912bfe7b7c..c631f42aa0 100644 --- a/packages/core/src/change_detection/differs/iterable_differs.ts +++ b/packages/core/src/change_detection/differs/iterable_differs.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {InjectableDef, defineInjectable} from '../../di/defs'; +import {defineInjectable} from '../../di/defs'; import {Optional, SkipSelf} from '../../di/metadata'; import {StaticProvider} from '../../di/provider'; import {DefaultIterableDifferFactory} from '../differs/default_iterable_differ'; @@ -137,7 +137,7 @@ export interface IterableDifferFactory { * */ export class IterableDiffers { - static ngInjectableDef: InjectableDef = defineInjectable({ + static ngInjectableDef = defineInjectable({ providedIn: 'root', factory: () => new IterableDiffers([new DefaultIterableDifferFactory()]) }); diff --git a/packages/core/src/di.ts b/packages/core/src/di.ts index e866ec2c02..62d3c4a38e 100644 --- a/packages/core/src/di.ts +++ b/packages/core/src/di.ts @@ -13,7 +13,7 @@ */ export * from './di/metadata'; -export * from './di/defs'; +export {InjectableType, InjectorType, defineInjectable, defineInjector} from './di/defs'; export {forwardRef, resolveForwardRef, ForwardRefFn} from './di/forward_ref'; export {Injectable, InjectableDecorator, InjectableProvider} from './di/injectable'; export {inject, InjectFlags, INJECTOR, Injector} from './di/injector'; diff --git a/packages/core/src/di/defs.ts b/packages/core/src/di/defs.ts index 9cfa828cbd..77f46d1f99 100644 --- a/packages/core/src/di/defs.ts +++ b/packages/core/src/di/defs.ts @@ -20,9 +20,7 @@ import {ClassProvider, ClassSansProvider, ConstructorProvider, ConstructorSansPr * `InjectorDef`, `NgModule`, or a special scope (e.g. `'root'`). A value of `null` indicates * that the injectable does not belong to any scope. * - * This type is typically generated by the Angular compiler, but can be hand-written if needed. - * - * @experimental + * NOTE: This is a private type and should not be exported */ export interface InjectableDef { /** @@ -54,7 +52,7 @@ export interface InjectableDef { * structure of providers with a defined priority (identically to how `NgModule`s also have * an import/dependency structure). * - * @experimental + * NOTE: This is a private type and should not be exported */ export interface InjectorDef { factory: () => T; @@ -75,7 +73,12 @@ export interface InjectorDef { * * @experimental */ -export interface InjectableType extends Type { ngInjectableDef: InjectableDef; } +export interface InjectableType extends Type { + /** + * Opaque type whose structure is highly version dependent. Do not rely on any properties. + */ + ngInjectableDef: never; +} /** * A type which has an `InjectorDef` static field. @@ -84,7 +87,12 @@ export interface InjectableType extends Type { ngInjectableDef: Injectable * * @experimental */ -export interface InjectorType extends Type { ngInjectorDef: InjectorDef; } +export interface InjectorType extends Type { + /** + * Opaque type whose structure is highly version dependent. Do not rely on any properties. + */ + ngInjectorDef: never; +} /** * Describes the `InjectorDef` equivalent of a `ModuleWithProviders`, an `InjectorDefType` with an @@ -92,7 +100,7 @@ export interface InjectorType extends Type { ngInjectorDef: InjectorDef * * Objects of this type can be listed in the imports section of an `InjectorDef`. * - * @experimental + * NOTE: This is a private type and should not be exported */ export interface InjectorTypeWithProviders { ngModule: InjectorType; @@ -120,12 +128,10 @@ export interface InjectorTypeWithProviders { export function defineInjectable(opts: { providedIn?: Type| 'root' | 'any' | null, factory: () => T, -}): InjectableDef { - return { - providedIn: opts.providedIn as any || null, - factory: opts.factory, - value: undefined, - }; +}): never { + return ({ + providedIn: opts.providedIn as any || null, factory: opts.factory, value: undefined, + } as InjectableDef) as never; } /** @@ -149,10 +155,8 @@ export function defineInjectable(opts: { * @experimental */ export function defineInjector(options: {factory: () => any, providers?: any[], imports?: any[]}): - InjectorDef { - return { - factory: options.factory, - providers: options.providers || [], - imports: options.imports || [], - }; + never { + return ({ + factory: options.factory, providers: options.providers || [], imports: options.imports || [], + } as InjectorDef) as never; } diff --git a/packages/core/src/di/injection_token.ts b/packages/core/src/di/injection_token.ts index 080ee0188e..364e0b9c7b 100644 --- a/packages/core/src/di/injection_token.ts +++ b/packages/core/src/di/injection_token.ts @@ -52,7 +52,7 @@ export class InjectionToken { /** @internal */ readonly ngMetadataName = 'InjectionToken'; - readonly ngInjectableDef: InjectableDef|undefined; + readonly ngInjectableDef: never|undefined; constructor(protected _desc: string, options?: { providedIn?: Type| 'root' | null, @@ -71,6 +71,4 @@ export class InjectionToken { toString(): string { return `InjectionToken ${this._desc}`; } } -export interface InjectableDefToken extends InjectionToken { - ngInjectableDef: InjectableDef; -} +export interface InjectableDefToken extends InjectionToken { ngInjectableDef: never; } diff --git a/packages/core/src/di/r3_injector.ts b/packages/core/src/di/r3_injector.ts index a3d679b762..9305d4b796 100644 --- a/packages/core/src/di/r3_injector.ts +++ b/packages/core/src/di/r3_injector.ts @@ -326,7 +326,7 @@ export class R3Injector { } function injectableDefRecord(token: Type| InjectionToken): Record { - const def = (token as InjectableType).ngInjectableDef; + const def = (token as InjectableType).ngInjectableDef as InjectableDef; if (def === undefined) { throw new Error(`Type ${stringify(token)} is missing an ngInjectableDef definition.`); } diff --git a/packages/core/src/render3/definition.ts b/packages/core/src/render3/definition.ts index e3851fe2fb..da3c36e1c5 100644 --- a/packages/core/src/render3/definition.ts +++ b/packages/core/src/render3/definition.ts @@ -162,7 +162,7 @@ export function defineComponent(componentDefinition: { * `PipeDefs`s. The function is necessary to be able to support forward declarations. */ pipes?: PipeTypesOrFactory | null; -}): ComponentDef { +}): never { const type = componentDefinition.type; const pipeTypes = componentDefinition.pipes !; const directiveTypes = componentDefinition.directives !; @@ -196,7 +196,7 @@ export function defineComponent(componentDefinition: { }; const feature = componentDefinition.features; feature && feature.forEach((fn) => fn(def)); - return def; + return def as never; } export function extractDirectiveDef(type: DirectiveType& ComponentType): @@ -400,7 +400,7 @@ export const defineDirective = defineComponent as any as(directiveDefinition: * See: {@link Directive.exportAs} */ exportAs?: string; -}) => DirectiveDef; +}) => never; /** * Create a pipe definition object. @@ -428,11 +428,11 @@ export function definePipe(pipeDef: { /** Whether the pipe is pure. */ pure?: boolean -}): PipeDef { - return >{ +}): never { + return (>{ name: pipeDef.name, n: pipeDef.factory, pure: pipeDef.pure !== false, onDestroy: pipeDef.type.prototype.ngOnDestroy || null - }; + }) as never; } diff --git a/packages/core/src/render3/interfaces/definition.ts b/packages/core/src/render3/interfaces/definition.ts index beb1804d73..4395efdca8 100644 --- a/packages/core/src/render3/interfaces/definition.ts +++ b/packages/core/src/render3/interfaces/definition.ts @@ -38,13 +38,13 @@ export const enum RenderFlags { * A subclass of `Type` which has a static `ngComponentDef`:`ComponentDef` field making it * consumable for rendering. */ -export interface ComponentType extends Type { ngComponentDef: ComponentDef; } +export interface ComponentType extends Type { ngComponentDef: never; } /** * A subclass of `Type` which has a static `ngDirectiveDef`:`DirectiveDef` field making it * consumable for rendering. */ -export interface DirectiveType extends Type { ngDirectiveDef: DirectiveDef; } +export interface DirectiveType extends Type { ngDirectiveDef: never; } export const enum DirectiveDefFlags {ContentQuery = 0b10} @@ -52,7 +52,7 @@ export const enum DirectiveDefFlags {ContentQuery = 0b10} * A subclass of `Type` which has a static `ngPipeDef`:`PipeDef` field making it * consumable for rendering. */ -export interface PipeType extends Type { ngPipeDef: PipeDef; } +export interface PipeType extends Type { ngPipeDef: never; } /** * Runtime link information for Directives. diff --git a/packages/core/test/bundling/injection/usage.ts b/packages/core/test/bundling/injection/usage.ts index 669aae806b..01d7fdce24 100644 --- a/packages/core/test/bundling/injection/usage.ts +++ b/packages/core/test/bundling/injection/usage.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {InjectableDef, Injector, InjectorDef, createInjector, defineInjectable, defineInjector} from '@angular/core'; +import {Injector, createInjector, defineInjectable, defineInjector} from '@angular/core'; export class RootService { static ngInjectableDef = defineInjectable({ diff --git a/packages/core/test/render3/compiler_canonical/back_patch_types_specs.ts b/packages/core/test/render3/compiler_canonical/back_patch_types_specs.ts index 180d555bb9..1f8a28d77c 100644 --- a/packages/core/test/render3/compiler_canonical/back_patch_types_specs.ts +++ b/packages/core/test/render3/compiler_canonical/back_patch_types_specs.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {Component, ContentChild, Directive, Injectable, Injector, InjectorDef, Input, NgModule, NgModuleFactory, NgModuleRef, OnDestroy, Optional, Pipe, PipeTransform, QueryList, SimpleChanges, TemplateRef, Type, ViewChild, ViewContainerRef, defineInjector} from '../../../src/core'; +import {Component, ContentChild, Directive, Injectable, Injector, Input, NgModule, NgModuleFactory, NgModuleRef, OnDestroy, Optional, Pipe, PipeTransform, QueryList, SimpleChanges, TemplateRef, Type, ViewChild, ViewContainerRef, defineInjector} from '../../../src/core'; import * as r3 from '../../../src/render3/index'; const details_elided = { diff --git a/packages/core/test/render3/compiler_canonical/component_directives_spec.ts b/packages/core/test/render3/compiler_canonical/component_directives_spec.ts index 70fa2abbcf..65a49ecec9 100644 --- a/packages/core/test/render3/compiler_canonical/component_directives_spec.ts +++ b/packages/core/test/render3/compiler_canonical/component_directives_spec.ts @@ -8,8 +8,11 @@ import {ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, ContentChildren, Directive, HostBinding, HostListener, Injectable, Input, NgModule, OnDestroy, Optional, Pipe, PipeTransform, QueryList, SimpleChanges, TemplateRef, ViewChild, ViewChildren, ViewContainerRef} from '../../../src/core'; import * as $r3$ from '../../../src/core_render3_private_export'; +import {ComponentDef} from '../../../src/render3/interfaces/definition'; import {renderComponent, toHtml} from '../render_util'; + + /// See: `normative.md` describe('components & directives', () => { type $RenderFlags$ = $r3$.ɵRenderFlags; @@ -76,8 +79,8 @@ describe('components & directives', () => { } // NON-NORMATIVE (done by defineNgModule) - MyComponent.ngComponentDef.directiveDefs = - [ChildComponent.ngComponentDef, SomeDirective.ngDirectiveDef]; + (MyComponent.ngComponentDef as ComponentDef).directiveDefs = + [(ChildComponent.ngComponentDef as ComponentDef), SomeDirective.ngDirectiveDef]; // /NON-NORMATIVE expect(renderComp(MyComponent)).toEqual('child-view!'); @@ -126,7 +129,7 @@ describe('components & directives', () => { } // NON-NORMATIVE (done by defineNgModule) - MyApp.ngComponentDef.directiveDefs = [HostBindingDir.ngDirectiveDef]; + (MyApp.ngComponentDef as ComponentDef).directiveDefs = [HostBindingDir.ngDirectiveDef]; // /NON-NORMATIVE expect(renderComp(MyApp)).toEqual(`
`); @@ -177,7 +180,7 @@ describe('components & directives', () => { } // NON-NORMATIVE (done by defineNgModule) - MyApp.ngComponentDef.directiveDefs = [HostListenerDir.ngDirectiveDef]; + (MyApp.ngComponentDef as ComponentDef).directiveDefs = [HostListenerDir.ngDirectiveDef]; // /NON-NORMATIVE expect(renderComp(MyApp)).toEqual(``); @@ -222,7 +225,7 @@ describe('components & directives', () => { } // NON-NORMATIVE (done by defineNgModule) - MyApp.ngComponentDef.directiveDefs = [HostAttributeDir.ngDirectiveDef]; + (MyApp.ngComponentDef as ComponentDef).directiveDefs = [HostAttributeDir.ngDirectiveDef]; // /NON-NORMATIVE expect(renderComp(MyApp)).toEqual(`
`); @@ -270,7 +273,7 @@ describe('components & directives', () => { } // NON-NORMATIVE (done by defineNgModule) - MyApp.ngComponentDef.directiveDefs = [HostBindingDir.ngDirectiveDef]; + (MyApp.ngComponentDef as ComponentDef).directiveDefs = [HostBindingDir.ngDirectiveDef]; // /NON-NORMATIVE expect(renderComp(MyApp)).toEqual(`
`); @@ -333,7 +336,8 @@ describe('components & directives', () => { } // NON-NORMATIVE (done by defineNgModule) - MyApp.ngComponentDef.directiveDefs = [MyComp.ngComponentDef]; + (MyApp.ngComponentDef as ComponentDef).directiveDefs = + [(MyComp.ngComponentDef as ComponentDef)]; // /NON-NORMATIVE expect(renderComp(MyApp)).toEqual(`some name`); @@ -463,7 +467,8 @@ describe('components & directives', () => { } // NON-NORMATIVE (done by defineNgModule) - MyApp.ngComponentDef.directiveDefs = [MyArrayComp.ngComponentDef]; + (MyApp.ngComponentDef as ComponentDef).directiveDefs = + [(MyArrayComp.ngComponentDef as ComponentDef)]; // /NON-NORMATIVE expect(renderComp(MyApp)).toEqual(`Nancy Bess`); @@ -507,7 +512,8 @@ describe('components & directives', () => { } // NON-NORMATIVE (done by defineNgModule) - MyApp.ngComponentDef.directiveDefs = [MyArrayComp.ngComponentDef]; + (MyApp.ngComponentDef as ComponentDef).directiveDefs = + [(MyArrayComp.ngComponentDef as ComponentDef)]; // /NON-NORMATIVE expect(renderComp(MyApp)).toEqual(`NANCY Bess`); @@ -567,7 +573,8 @@ describe('components & directives', () => { } // NON-NORMATIVE (done by defineNgModule) - MyApp.ngComponentDef.directiveDefs = [MyComp.ngComponentDef]; + (MyApp.ngComponentDef as ComponentDef).directiveDefs = + [(MyComp.ngComponentDef as ComponentDef)]; // /NON-NORMATIVE expect(renderComp(MyApp)).toEqual(`3`); @@ -609,7 +616,8 @@ describe('components & directives', () => { } // NON-NORMATIVE (done by defineNgModule) - MyApp.ngComponentDef.directiveDefs = [MyArrayComp.ngComponentDef]; + (MyApp.ngComponentDef as ComponentDef).directiveDefs = + [(MyArrayComp.ngComponentDef as ComponentDef)]; // /NON-NORMATIVE expect(renderComp(MyApp)).toEqual(`Nancy Bess`); @@ -721,7 +729,8 @@ describe('components & directives', () => { } // NON-NORMATIVE (done by defineNgModule) - MyApp.ngComponentDef.directiveDefs = [MyComp.ngComponentDef]; + (MyApp.ngComponentDef as ComponentDef).directiveDefs = + [(MyComp.ngComponentDef as ComponentDef)]; // /NON-NORMATIVE expect(renderComp(MyApp)).toEqual(`start-abcde-middle-fghi-end`); @@ -795,7 +804,8 @@ describe('components & directives', () => { } // NON-NORMATIVE (done by defineNgModule) - MyApp.ngComponentDef.directiveDefs = [ObjectComp.ngComponentDef]; + (MyApp.ngComponentDef as ComponentDef).directiveDefs = + [(ObjectComp.ngComponentDef as ComponentDef)]; // /NON-NORMATIVE expect(renderComp(MyApp)).toEqual(`

500

slide

`); @@ -882,7 +892,8 @@ describe('components & directives', () => { } // NON-NORMATIVE (done by defineNgModule) - MyApp.ngComponentDef.directiveDefs = [NestedComp.ngComponentDef]; + (MyApp.ngComponentDef as ComponentDef).directiveDefs = + [(NestedComp.ngComponentDef as ComponentDef)]; // /NON-NORMATIVE expect(renderComp(MyApp)) diff --git a/packages/core/test/render3/compiler_canonical/elements_spec.ts b/packages/core/test/render3/compiler_canonical/elements_spec.ts index 46f2f4489d..1155b8b229 100644 --- a/packages/core/test/render3/compiler_canonical/elements_spec.ts +++ b/packages/core/test/render3/compiler_canonical/elements_spec.ts @@ -7,10 +7,13 @@ */ import {browserDetection} from '@angular/platform-browser/testing/src/browser_util'; + import {ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, ContentChildren, Directive, HostBinding, HostListener, Injectable, Input, NgModule, OnDestroy, Optional, Pipe, PipeTransform, QueryList, SimpleChanges, TemplateRef, ViewChild, ViewChildren, ViewContainerRef} from '../../../src/core'; import * as $r3$ from '../../../src/core_render3_private_export'; +import {ComponentDef} from '../../../src/render3/interfaces/definition'; import {ComponentFixture, renderComponent, toHtml} from '../render_util'; + /// See: `normative.md` describe('elements', () => { // Saving type as $any$, etc to simplify testing for compiler, as types aren't saved @@ -103,7 +106,7 @@ describe('elements', () => { } // NON-NORMATIVE - LocalRefComp.ngComponentDef.directiveDefs = () => [Dir.ngDirectiveDef]; + (LocalRefComp.ngComponentDef as ComponentDef).directiveDefs = () => [Dir.ngDirectiveDef]; // /NON-NORMATIVE const fixture = new ComponentFixture(LocalRefComp); diff --git a/packages/core/test/render3/compiler_canonical/injection_spec.ts b/packages/core/test/render3/compiler_canonical/injection_spec.ts index 3c61d4901c..d8af9c2810 100644 --- a/packages/core/test/render3/compiler_canonical/injection_spec.ts +++ b/packages/core/test/render3/compiler_canonical/injection_spec.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {Attribute, ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, ContentChildren, Directive, HostBinding, HostListener, INJECTOR, Inject, InjectFlags, Injectable, InjectableDef, Injector, InjectorDef, Input, NgModule, OnDestroy, Optional, Pipe, PipeTransform, QueryList, SimpleChanges, SkipSelf, TemplateRef, ViewChild, ViewChildren, ViewContainerRef, defineInjectable, defineInjector, inject} from '../../../src/core'; +import {Attribute, ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, ContentChildren, Directive, HostBinding, HostListener, INJECTOR, Inject, InjectFlags, Injectable, Injector, Input, NgModule, OnDestroy, Optional, Pipe, PipeTransform, QueryList, SimpleChanges, SkipSelf, TemplateRef, ViewChild, ViewChildren, ViewContainerRef, defineInjectable, defineInjector, inject} from '../../../src/core'; import * as $r3$ from '../../../src/core_render3_private_export'; import {renderComponent, toHtml} from '../render_util'; diff --git a/packages/core/test/render3/compiler_canonical/life_cycle_spec.ts b/packages/core/test/render3/compiler_canonical/life_cycle_spec.ts index c3e2a4dbb2..0f4f5113a0 100644 --- a/packages/core/test/render3/compiler_canonical/life_cycle_spec.ts +++ b/packages/core/test/render3/compiler_canonical/life_cycle_spec.ts @@ -8,8 +8,10 @@ import {ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, ContentChildren, Directive, HostBinding, HostListener, Injectable, Input, NgModule, OnDestroy, Optional, Pipe, PipeTransform, QueryList, SimpleChanges, TemplateRef, ViewChild, ViewChildren, ViewContainerRef} from '../../../src/core'; import * as $r3$ from '../../../src/core_render3_private_export'; +import {ComponentDef} from '../../../src/render3/interfaces/definition'; import {renderComponent, toHtml} from '../render_util'; + /// See: `normative.md` describe('lifecycle hooks', () => { let events: string[] = []; @@ -83,7 +85,7 @@ describe('lifecycle hooks', () => { } // NON-NORMATIVE - SimpleLayout.ngComponentDef.directiveDefs = [LifecycleComp.ngComponentDef]; + (SimpleLayout.ngComponentDef as ComponentDef).directiveDefs = [LifecycleComp.ngComponentDef]; // /NON-NORMATIVE it('should gen hooks with a few simple components', () => { diff --git a/packages/core/test/render3/compiler_canonical/patch_types_spec.ts b/packages/core/test/render3/compiler_canonical/patch_types_spec.ts index 188de69b32..85e42f5724 100644 --- a/packages/core/test/render3/compiler_canonical/patch_types_spec.ts +++ b/packages/core/test/render3/compiler_canonical/patch_types_spec.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {Component, ContentChild, Directive, Injectable, InjectableDef, InjectorDef, Input, NgModule, OnDestroy, Optional, Pipe, PipeTransform, QueryList, SimpleChanges, TemplateRef, Type, ViewChild, ViewContainerRef, defineInjectable, defineInjector} from '../../../src/core'; +import {Component, ContentChild, Directive, Injectable, Input, NgModule, OnDestroy, Optional, Pipe, PipeTransform, QueryList, SimpleChanges, TemplateRef, Type, ViewChild, ViewContainerRef, defineInjectable, defineInjector} from '../../../src/core'; import * as r3 from '../../../src/render3/index'; diff --git a/packages/core/test/render3/compiler_canonical/pipes_spec.ts b/packages/core/test/render3/compiler_canonical/pipes_spec.ts index e8a526e83e..625b4e2c32 100644 --- a/packages/core/test/render3/compiler_canonical/pipes_spec.ts +++ b/packages/core/test/render3/compiler_canonical/pipes_spec.ts @@ -8,8 +8,10 @@ import {ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, ContentChildren, Directive, HostBinding, HostListener, Injectable, Input, NgModule, OnDestroy, Optional, Pipe, PipeTransform, QueryList, SimpleChanges, TemplateRef, ViewChild, ViewChildren, ViewContainerRef} from '../../../src/core'; import * as $r3$ from '../../../src/core_render3_private_export'; +import {ComponentDef} from '../../../src/render3/interfaces/definition'; import {containerEl, renderComponent, toHtml} from '../render_util'; + /// See: `normative.md` describe('pipes', () => { type $any$ = any; @@ -96,7 +98,8 @@ describe('pipes', () => { } // NON-NORMATIVE - MyApp.ngComponentDef.pipeDefs = () => [MyPurePipe.ngPipeDef, MyPipe.ngPipeDef]; + (MyApp.ngComponentDef as ComponentDef).pipeDefs = + () => [MyPurePipe.ngPipeDef, MyPipe.ngPipeDef]; // /NON-NORMATIVE let myApp: MyApp = renderComponent(MyApp); @@ -189,8 +192,8 @@ describe('pipes', () => { } // NON-NORMATIVE - MyApp.ngComponentDef.directiveDefs = [OneTimeIf.ngDirectiveDef]; - MyApp.ngComponentDef.pipeDefs = [MyPurePipe.ngPipeDef]; + (MyApp.ngComponentDef as ComponentDef).directiveDefs = [OneTimeIf.ngDirectiveDef]; + (MyApp.ngComponentDef as ComponentDef).pipeDefs = [MyPurePipe.ngPipeDef]; // /NON-NORMATIVE let myApp: MyApp = renderComponent(MyApp); diff --git a/packages/core/test/render3/compiler_canonical/query_spec.ts b/packages/core/test/render3/compiler_canonical/query_spec.ts index ebe310a4d2..d3e19413e3 100644 --- a/packages/core/test/render3/compiler_canonical/query_spec.ts +++ b/packages/core/test/render3/compiler_canonical/query_spec.ts @@ -8,8 +8,10 @@ import {ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, ContentChildren, Directive, HostBinding, HostListener, Injectable, Input, NgModule, OnDestroy, Optional, Pipe, PipeTransform, QueryList, SimpleChanges, TemplateRef, ViewChild, ViewChildren, ViewContainerRef} from '../../../src/core'; import * as $r3$ from '../../../src/core_render3_private_export'; +import {ComponentDef} from '../../../src/render3/interfaces/definition'; import {renderComponent, toHtml} from '../render_util'; + /// See: `normative.md` describe('queries', () => { type $RenderFlags$ = $r3$.ɵRenderFlags; @@ -70,7 +72,8 @@ describe('queries', () => { } // NON-NORMATIVE - ViewQueryComponent.ngComponentDef.directiveDefs = [SomeDirective.ngDirectiveDef]; + (ViewQueryComponent.ngComponentDef as ComponentDef).directiveDefs = + [SomeDirective.ngDirectiveDef]; // /NON-NORMATIVE const viewQueryComp = renderComponent(ViewQueryComponent); @@ -154,7 +157,7 @@ describe('queries', () => { } // NON-NORMATIVE - MyApp.ngComponentDef.directiveDefs = + (MyApp.ngComponentDef as ComponentDef).directiveDefs = [ContentQueryComponent.ngComponentDef, SomeDirective.ngDirectiveDef]; // /NON-NORMATIVE diff --git a/packages/core/test/render3/compiler_canonical/small_app_spec.ts b/packages/core/test/render3/compiler_canonical/small_app_spec.ts index 25807caeb0..93e795d829 100644 --- a/packages/core/test/render3/compiler_canonical/small_app_spec.ts +++ b/packages/core/test/render3/compiler_canonical/small_app_spec.ts @@ -7,7 +7,7 @@ */ import {NgForOf, NgForOfContext} from '@angular/common'; -import {Component, ContentChild, Directive, EventEmitter, Injectable, InjectableDef, InjectorDef, Input, NgModule, OnDestroy, Optional, Output, Pipe, PipeTransform, QueryList, SimpleChanges, TemplateRef, Type, ViewChild, ViewContainerRef, defineInjectable, defineInjector} from '@angular/core'; +import {Component, ContentChild, Directive, EventEmitter, Injectable, Input, NgModule, OnDestroy, Optional, Output, Pipe, PipeTransform, QueryList, SimpleChanges, TemplateRef, Type, ViewChild, ViewContainerRef, defineInjectable, defineInjector} from '@angular/core'; import {withBody} from '@angular/core/testing'; import * as r3 from '../../../src/render3/index'; @@ -96,7 +96,7 @@ class ToDoAppComponent { } // NON-NORMATIVE -ToDoAppComponent.ngComponentDef.directiveDefs = () => +(ToDoAppComponent.ngComponentDef as r3.ComponentDef).directiveDefs = () => [ToDoItemComponent.ngComponentDef, (NgForOf as r3.DirectiveType>).ngDirectiveDef]; // /NON-NORMATIVE diff --git a/packages/core/test/render3/compiler_canonical/template_variables_spec.ts b/packages/core/test/render3/compiler_canonical/template_variables_spec.ts index 6cf32e38f2..26c198b24c 100644 --- a/packages/core/test/render3/compiler_canonical/template_variables_spec.ts +++ b/packages/core/test/render3/compiler_canonical/template_variables_spec.ts @@ -8,8 +8,10 @@ import {ChangeDetectionStrategy, ChangeDetectorRef, Component, ContentChild, ContentChildren, Directive, HostBinding, HostListener, Injectable, Input, NgModule, OnDestroy, Optional, Pipe, PipeTransform, QueryList, SimpleChanges, TemplateRef, ViewChild, ViewChildren, ViewContainerRef} from '../../../src/core'; import * as $r3$ from '../../../src/core_render3_private_export'; +import {ComponentDef} from '../../../src/render3/interfaces/definition'; import {renderComponent, toHtml} from '../render_util'; + /// See: `normative.md` describe('template variables', () => { type $any$ = any; @@ -123,7 +125,8 @@ describe('template variables', () => { } // NON-NORMATIVE - MyComponent.ngComponentDef.directiveDefs = [ForOfDirective.ngDirectiveDef]; + (MyComponent.ngComponentDef as ComponentDef).directiveDefs = + [ForOfDirective.ngDirectiveDef]; // /NON-NORMATIVE // TODO(chuckj): update when the changes to enable ngForOf lands. diff --git a/packages/core/test/render3/component_spec.ts b/packages/core/test/render3/component_spec.ts index 4b54a7e3ea..7b7026e9a2 100644 --- a/packages/core/test/render3/component_spec.ts +++ b/packages/core/test/render3/component_spec.ts @@ -11,7 +11,7 @@ import {ComponentFactory, DoCheck, ViewEncapsulation, createInjector, defineInje import {getRenderedText} from '../../src/render3/component'; import {LifecycleHooksFeature, defineComponent, directiveInject, markDirty} from '../../src/render3/index'; import {bind, container, containerRefreshEnd, containerRefreshStart, elementEnd, elementProperty, elementStart, embeddedViewEnd, embeddedViewStart, text, textBinding, tick} from '../../src/render3/instructions'; -import {RenderFlags} from '../../src/render3/interfaces/definition'; +import {ComponentDef, DirectiveDef, RenderFlags} from '../../src/render3/interfaces/definition'; import {createRendererType2} from '../../src/view/index'; import {getRendererFactory2} from './imported_renderer2'; @@ -346,7 +346,8 @@ describe('recursive components', () => { }); } - TreeComponent.ngComponentDef.directiveDefs = () => [TreeComponent.ngComponentDef]; + (TreeComponent.ngComponentDef as ComponentDef).directiveDefs = + () => [TreeComponent.ngComponentDef]; function _buildTree(currDepth: number): TreeNode { const children = currDepth < 2 ? _buildTree(currDepth + 1) : null; diff --git a/packages/core/test/render3/define_spec.ts b/packages/core/test/render3/define_spec.ts index 94ab58d984..7e03c7460d 100644 --- a/packages/core/test/render3/define_spec.ts +++ b/packages/core/test/render3/define_spec.ts @@ -7,7 +7,7 @@ */ import {DoCheck, OnChanges, SimpleChanges} from '../../src/core'; -import {NgOnChangesFeature, defineDirective} from '../../src/render3/index'; +import {DirectiveDef, NgOnChangesFeature, defineDirective} from '../../src/render3/index'; describe('define', () => { describe('component', () => { @@ -36,14 +36,15 @@ describe('define', () => { }); } - const myDir = MyDirective.ngDirectiveDef.factory() as MyDirective; + const myDir = + (MyDirective.ngDirectiveDef as DirectiveDef).factory() as MyDirective; myDir.valA = 'first'; expect(myDir.valA).toEqual('first'); myDir.valB = 'second'; expect(myDir.log).toEqual(['second']); expect(myDir.valB).toEqual('works'); myDir.log.length = 0; - MyDirective.ngDirectiveDef.doCheck !.call(myDir); + (MyDirective.ngDirectiveDef as DirectiveDef).doCheck !.call(myDir); expect(myDir.log).toEqual([ 'ngOnChanges', 'valA', 'initValue', 'first', 'valB', undefined, 'second', 'ngDoCheck' ]); diff --git a/tools/public_api_guard/core/core.d.ts b/tools/public_api_guard/core/core.d.ts index 40a2081fbd..d781017237 100644 --- a/tools/public_api_guard/core/core.d.ts +++ b/tools/public_api_guard/core/core.d.ts @@ -244,14 +244,14 @@ export declare class DefaultIterableDiffer implements IterableDiffer, Iter export declare function defineInjectable(opts: { providedIn?: Type | 'root' | 'any' | null; factory: () => T; -}): InjectableDef; +}): never; /** @experimental */ export declare function defineInjector(options: { factory: () => any; providers?: any[]; imports?: any[]; -}): InjectorDef; +}): never; /** @experimental */ export declare function destroyPlatform(): void; @@ -354,19 +354,12 @@ export interface InjectableDecorator { } & InjectableProvider): Injectable; } -/** @experimental */ -export interface InjectableDef { - factory: () => T; - providedIn: InjectorType | 'root' | 'any' | null; - value: T | undefined; -} - /** @experimental */ export declare type InjectableProvider = ValueSansProvider | ExistingSansProvider | StaticClassSansProvider | ConstructorSansProvider | FactorySansProvider | ClassSansProvider; /** @experimental */ export interface InjectableType extends Type { - ngInjectableDef: InjectableDef; + ngInjectableDef: never; } export interface InjectDecorator { @@ -384,7 +377,7 @@ export declare const enum InjectFlags { export declare class InjectionToken { protected _desc: string; - readonly ngInjectableDef: InjectableDef | undefined; + readonly ngInjectableDef: never | undefined; constructor(_desc: string, options?: { providedIn?: Type | 'root' | null; factory: () => T; @@ -397,7 +390,7 @@ export declare abstract class Injector { /** @deprecated */ abstract get(token: any, notFoundValue?: any): any; static NULL: Injector; static THROW_IF_NOT_FOUND: Object; - static ngInjectableDef: InjectableDef; + static ngInjectableDef: never; /** @deprecated */ static create(providers: StaticProvider[], parent?: Injector): Injector; static create(options: { providers: StaticProvider[]; @@ -409,22 +402,9 @@ export declare abstract class Injector { /** @experimental */ export declare const INJECTOR: InjectionToken; -/** @experimental */ -export interface InjectorDef { - factory: () => T; - imports: (InjectorType | InjectorTypeWithProviders)[]; - providers: (Type | ValueProvider | ExistingProvider | FactoryProvider | ConstructorProvider | StaticClassProvider | ClassProvider | any[])[]; -} - /** @experimental */ export interface InjectorType extends Type { - ngInjectorDef: InjectorDef; -} - -/** @experimental */ -export interface InjectorTypeWithProviders { - ngModule: InjectorType; - providers?: (Type | ValueProvider | ExistingProvider | FactoryProvider | ConstructorProvider | StaticClassProvider | ClassProvider | any[])[]; + ngInjectorDef: never; } export declare const Input: InputDecorator; @@ -462,7 +442,7 @@ export declare class IterableDiffers { /** @deprecated */ factories: IterableDifferFactory[]; constructor(factories: IterableDifferFactory[]); find(iterable: any): IterableDifferFactory; - static ngInjectableDef: InjectableDef; + static ngInjectableDef: never; static create(factories: IterableDifferFactory[], parent?: IterableDiffers): IterableDiffers; static extend(factories: IterableDifferFactory[]): StaticProvider; }