Skip to content

Commit

Permalink
feat(modal): set strong typing in componentInstance getter property
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljota authored and Michael De Abreu committed Oct 4, 2019
1 parent 36918b5 commit ffc129e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/modal/modal-ref.ts
Expand Up @@ -30,7 +30,7 @@ export class NgbActiveModal {
/**
* A reference to the newly opened modal returned by the `NgbModal.open()` method.
*/
export class NgbModalRef {
export class NgbModalRef<T = any> {
private _resolve: (result?: any) => void;
private _reject: (reason?: any) => void;

Expand All @@ -39,7 +39,7 @@ export class NgbModalRef {
*
* When a `TemplateRef` is used as the content or when the modal is closed, will return `undefined`.
*/
get componentInstance(): any {
get componentInstance(): T extends new (...args: any[]) => any ? InstanceType<T> : undefined {
if (this._contentRef && this._contentRef.componentRef) {
return this._contentRef.componentRef.instance;
}
Expand Down
6 changes: 3 additions & 3 deletions src/modal/modal-stack.ts
Expand Up @@ -44,7 +44,7 @@ export class NgbModalStack {
});
}

open(moduleCFR: ComponentFactoryResolver, contentInjector: Injector, content: any, options): NgbModalRef {
open<T>(moduleCFR: ComponentFactoryResolver, contentInjector: Injector, content: T, options): NgbModalRef<T> {
const containerEl =
isDefined(options.container) ? this._document.querySelector(options.container) : this._document.body;
const renderer = this._rendererFactory.createRenderer(null, null);
Expand All @@ -68,7 +68,7 @@ export class NgbModalStack {
let backdropCmptRef: ComponentRef<NgbModalBackdrop> =
options.backdrop !== false ? this._attachBackdrop(moduleCFR, containerEl) : null;
let windowCmptRef: ComponentRef<NgbModalWindow> = this._attachWindowComponent(moduleCFR, containerEl, contentRef);
let ngbModalRef: NgbModalRef = new NgbModalRef(windowCmptRef, contentRef, backdropCmptRef, options.beforeDismiss);
let ngbModalRef: NgbModalRef<T> = new NgbModalRef(windowCmptRef, contentRef, backdropCmptRef, options.beforeDismiss);

this._registerModalRef(ngbModalRef);
this._registerWindowCmpt(windowCmptRef);
Expand Down Expand Up @@ -197,7 +197,7 @@ export class NgbModalStack {
this._ariaHiddenValues.clear();
}

private _registerModalRef(ngbModalRef: NgbModalRef) {
private _registerModalRef<T>(ngbModalRef: NgbModalRef<T>) {
const unregisterModalRef = () => {
const index = this._modalRefs.indexOf(ngbModalRef);
if (index > -1) {
Expand Down
2 changes: 1 addition & 1 deletion src/modal/modal.spec.ts
Expand Up @@ -1087,7 +1087,7 @@ export class WithSkipTabindexFirstFocusableModalCmpt {
})
class TestComponent {
name = 'World';
openedModal: NgbModalRef;
openedModal: NgbModalRef<string>;
show = true;
@ViewChild('content', {static: true}) tplContent;
@ViewChild('destroyableContent', {static: true}) tplDestroyableContent;
Expand Down
2 changes: 1 addition & 1 deletion src/modal/modal.ts
Expand Up @@ -25,7 +25,7 @@ export class NgbModal {
*
* Also see the [`NgbModalOptions`](#/components/modal/api#NgbModalOptions) for the list of supported options.
*/
open(content: any, options: NgbModalOptions = {}): NgbModalRef {
open<T>(content: T, options: NgbModalOptions = {}): NgbModalRef<T> {
const combinedOptions = Object.assign({}, this._config, options);
return this._modalStack.open(this._moduleCFR, this._injector, content, combinedOptions);
}
Expand Down

0 comments on commit ffc129e

Please sign in to comment.