refactor(core): improve forwardRef typings (#53880)

This commit improves the forwardRef typings for better
type safety and inference.

PR Close #53880
This commit is contained in:
Pawel Kozlowski
2024-01-11 17:12:57 +01:00
committed by Dylan Hunn
parent 1f8c53cd0c
commit af6f6e6448
3 changed files with 9 additions and 10 deletions
+3 -3
View File
@@ -647,12 +647,12 @@ export interface FactorySansProvider {
}
// @public
export function forwardRef(forwardRefFn: ForwardRefFn): Type<any>;
export function forwardRef<T>(forwardRefFn: ForwardRefFn<T>): T;
// @public
export interface ForwardRefFn {
export interface ForwardRefFn<T> {
// (undocumented)
(): any;
(): T;
}
// @public (undocumented)
+4 -5
View File
@@ -6,7 +6,6 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Type} from '../interface/type';
import {getClosureSafeProperty} from '../util/property';
import {stringify} from '../util/stringify';
@@ -21,8 +20,8 @@ import {stringify} from '../util/stringify';
* {@example core/di/ts/forward_ref/forward_ref_spec.ts region='forward_ref_fn'}
* @publicApi
*/
export interface ForwardRefFn {
(): any;
export interface ForwardRefFn<T> {
(): T;
}
const __forward_ref__ = getClosureSafeProperty({__forward_ref__: getClosureSafeProperty});
@@ -66,12 +65,12 @@ const __forward_ref__ = getClosureSafeProperty({__forward_ref__: getClosureSafeP
*
* @publicApi
*/
export function forwardRef(forwardRefFn: ForwardRefFn): Type<any> {
export function forwardRef<T>(forwardRefFn: ForwardRefFn<T>): T {
(<any>forwardRefFn).__forward_ref__ = forwardRef;
(<any>forwardRefFn).toString = function() {
return stringify(this());
};
return (<Type<any>><any>forwardRefFn);
return (<T><any>forwardRefFn);
}
/**
@@ -7,7 +7,7 @@
*/
import {CommonModule, NgComponentOutlet} from '@angular/common';
import {Component, createEnvironmentInjector, Directive, EnvironmentInjector, forwardRef, inject, Injectable, Injector, Input, isStandalone, NgModule, NO_ERRORS_SCHEMA, OnInit, Pipe, PipeTransform, ViewChild, ViewContainerRef} from '@angular/core';
import {Component, createEnvironmentInjector, Directive, EnvironmentInjector, forwardRef, inject, Injectable, Injector, Input, isStandalone, NgModule, NO_ERRORS_SCHEMA, OnInit, Pipe, PipeTransform, Type, ViewChild, ViewContainerRef} from '@angular/core';
import {TestBed} from '@angular/core/testing';
describe('standalone components, directives, and pipes', () => {
@@ -584,7 +584,7 @@ describe('standalone components, directives, and pipes', () => {
@Component({
selector: 'test',
standalone: true,
imports: [forwardRef(() => null)],
imports: [forwardRef(() => null as any as Type<any>)],
template: '',
})
class TestComponent {