From af6f6e6448edffcd644b0bb6ce155c3ca4270971 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Thu, 11 Jan 2024 17:12:57 +0100 Subject: [PATCH] refactor(core): improve forwardRef typings (#53880) This commit improves the forwardRef typings for better type safety and inference. PR Close #53880 --- goldens/public-api/core/index.md | 6 +++--- packages/core/src/di/forward_ref.ts | 9 ++++----- packages/core/test/acceptance/standalone_spec.ts | 4 ++-- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/goldens/public-api/core/index.md b/goldens/public-api/core/index.md index 7fc215a521c..2a9151fa7e8 100644 --- a/goldens/public-api/core/index.md +++ b/goldens/public-api/core/index.md @@ -647,12 +647,12 @@ export interface FactorySansProvider { } // @public -export function forwardRef(forwardRefFn: ForwardRefFn): Type; +export function forwardRef(forwardRefFn: ForwardRefFn): T; // @public -export interface ForwardRefFn { +export interface ForwardRefFn { // (undocumented) - (): any; + (): T; } // @public (undocumented) diff --git a/packages/core/src/di/forward_ref.ts b/packages/core/src/di/forward_ref.ts index ff197f1a133..1e01cd17211 100644 --- a/packages/core/src/di/forward_ref.ts +++ b/packages/core/src/di/forward_ref.ts @@ -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; } 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 { +export function forwardRef(forwardRefFn: ForwardRefFn): T { (forwardRefFn).__forward_ref__ = forwardRef; (forwardRefFn).toString = function() { return stringify(this()); }; - return (>forwardRefFn); + return (forwardRefFn); } /** diff --git a/packages/core/test/acceptance/standalone_spec.ts b/packages/core/test/acceptance/standalone_spec.ts index 4d38d46058a..ed53deb8a89 100644 --- a/packages/core/test/acceptance/standalone_spec.ts +++ b/packages/core/test/acceptance/standalone_spec.ts @@ -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)], template: '', }) class TestComponent {