GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — refactor(core): add g3-only overloads to preserve generic token types by JeanMeche · Pull Request #71762 · angular/angular

Conversation

@JeanMeche

@JeanMeche JeanMeche commented Nov 2, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

In TypeScript, instantiation expressions on class constructors specialize construct signatures (new) while leaving prototype unspecialized. Because ProviderToken<T> includes AbstractType<T> ({prototype: T}), passing specialized generic tokens to inject(), Injector.get(), or signal queries caused TypeScript to infer T from prototype, widening the return type to any.

Tightening this inference is a breaking change for code relying on the implicit any return type (for example, s: GenericService<Foo> = inject(GenericService)). To roll this out safely in two steps, this PR introduces the stricter overloads behind g3-only markers so they take effect internally first, leaving external (3P) public types unchanged until the v23 breaking-change window opens:

  • inject(): add g3-only abstract new (...args: any[]) => T overloads (with and without InjectOptions).
  • Injector.get(): add g3-only abstract new (...args: any[]) => T overloads (non-optional, nullable, and default forms).
  • Signal queries (viewChild, viewChildren, contentChild, contentChildren): add g3-only abstract new (...args: any[]) => ... overloads for locators and read options.
  • Tests: add 3P tests verifying current any inference behavior is preserved, alongside TODO(v23) skipped tests to re-enable when the breaking change lands in 3P.

Related to #53894 and #48126 (will be closed once enabled in 3P during the v23 breaking-change window).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mauriziocescon reacted with hooray emoji wartab and mauriziocescon reacted with heart emoji mauriziocescon and SkyZeroZx reacted with rocket emoji SkyZeroZx reacted with eyes emoji
All reactions
  • 🎉 1 reaction
  • ❤️ 2 reactions
  • 🚀 2 reactions
  • 👀 1 reaction
@angular-robot angular-robot Bot added area: core Issues related to the framework runtime requires: TGP This PR requires a passing TGP before merging is allowed labels Nov 2, 2026
@ngbot ngbot Bot added this to the Backlog milestone Nov 2, 2026
@JeanMeche
JeanMeche force-pushed the fix/53894-inject-generic-type branch 4 times, most recently from 765149b to 169e53d Compare November 2, 2026 01:52
@JeanMeche JeanMeche added the state: blocked on G3 cleanup This change requires a G3 cleanup label Nov 2, 2026
@JeanMeche
JeanMeche force-pushed the fix/53894-inject-generic-type branch 12 times, most recently from 547fe92 to de1fc86 Compare November 2, 2026 10:53
@JeanMeche

JeanMeche commented Nov 2, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Type Inference Summary

Using abstract new (...args: any[]) => T (and InjectionToken<ReadT> for queries read option) allows instantiation expressions to preserve their type arguments without complex conditional types:

Call Before After
inject(GenericService) GenericService<any> GenericService<'a' | 'b'>
inject(GenericService<'a'>) GenericService<any> GenericService<'a'>
inject(AbstractGenericService<'x'>) AbstractGenericService<any> AbstractGenericService<'x'>
inject(AbstractDateAdapter<Date>) AbstractDateAdapter<any, any> AbstractDateAdapter<Date, any>
inject(TemplateRef<MyCtx>) TemplateRef<any> TemplateRef<MyCtx>
viewChild(MyDir<string>) Signal<MyDir<any> | undefined> Signal<MyDir<string> | undefined>
viewChild('ref', {read: ElementRef}) Signal<ElementRef<any> | undefined> Signal<ElementRef<any> | undefined>
viewChild('ref', {read: ElementRef<HTMLAnchorElement>}) Signal<ElementRef<any> | undefined> Signal<ElementRef<HTMLAnchorElement> | undefined>
viewChild('tpl', {read: TemplateRef<MyCtx>}) Signal<TemplateRef<any> | undefined> Signal<TemplateRef<MyCtx> | undefined>
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JeanMeche JeanMeche mentioned this pull request Nov 2, 2026
test(core): add tests for preserving generic type parameters when injecting or querying tokens #71774
Closed
JeanMeche added a commit to JeanMeche/angular that referenced this pull request Nov 2, 2026
@JeanMeche
test(core): add tests for preserving generic type parameters when inj…
14db32d
…ecting or querying tokens

Ports the test changes from angular#71762.
@JeanMeche
JeanMeche force-pushed the fix/53894-inject-generic-type branch from de1fc86 to 469230d Compare November 2, 2026 20:10
@SkyZeroZx

SkyZeroZx commented Nov 2, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

As a question, could this be considered a breaking change? or at least I think we might need a migration script after this is merged, since I've seen code like this many times in codebases

 private readonly dataService: DataService<MyItem> = inject(DataService);
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JeanMeche

JeanMeche commented Nov 2, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

It's definitely a breaking change. It's a large breakage inside Google3.

If we agree to land it. It will probably be a 2-stepper. (the 2nd step being the actual breaking change during the v23 window + a migration script alongside it).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JeanMeche
JeanMeche force-pushed the fix/53894-inject-generic-type branch from 469230d to 789887a Compare November 3, 2026 17:43
@JeanMeche JeanMeche removed the requires: TGP This PR requires a passing TGP before merging is allowed label Nov 3, 2026
@JeanMeche
JeanMeche force-pushed the fix/53894-inject-generic-type branch 2 times, most recently from e772785 to 6de5e46 Compare November 3, 2026 18:14
@JeanMeche
JeanMeche force-pushed the fix/53894-inject-generic-type branch 5 times, most recently from 72b3fb5 to ada8b0d Compare November 11, 2026 12:12
@JeanMeche JeanMeche mentioned this pull request Nov 24, 2026
v23 Breaking Changes Dashboard #72191
Open
29 tasks
@JeanMeche

JeanMeche commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Passing TGP

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JeanMeche
JeanMeche requested a review from thePunderWoman November 24, 2026 16:41
@JeanMeche
JeanMeche marked this pull request as ready for review November 24, 2026 16:41
@pullapprove
pullapprove Bot requested a review from kirjs November 24, 2026 16:41
@JeanMeche
JeanMeche force-pushed the fix/53894-inject-generic-type branch from ada8b0d to 81d2de1 Compare November 24, 2026 16:43
@JeanMeche
JeanMeche removed the request for review from kirjs November 24, 2026 16:45
@pullapprove
pullapprove Bot requested a review from kirjs November 24, 2026 16:45
@JeanMeche
JeanMeche force-pushed the fix/53894-inject-generic-type branch from 81d2de1 to 11ef300 Compare November 24, 2026 16:49
@JeanMeche JeanMeche changed the title fix(core): preserve generic type parameters when injecting or querying tokens refactor(core): add g3-only overloads to preserve generic token types Nov 24, 2026
@JeanMeche JeanMeche removed the state: blocked on G3 cleanup This change requires a G3 cleanup label Nov 24, 2026
@JeanMeche
JeanMeche force-pushed the fix/53894-inject-generic-type branch from 11ef300 to 9628aad Compare November 24, 2026 16:59
@JeanMeche
build: add Bazel g3_mode flag and acceptance_g3 test target
112c778
Adds a Bazel `--//tools/bazel:g3_mode` build setting and a `g3_test`
transition rule so Angular tests can be compiled and executed locally
and in CI with `g3-only` / `3p-only` sync markers active.

- **`tools/bazel/patches/rules_angular_g3_mode.patch`**: Supports
  `--ngG3Mode` in `rules_angular`'s compiler worker, applying
  `g3-only` / `3p-only` markers in-memory via `fs.readFile` with an
  isolated `FileCache` (zero overhead and zero cache pollution for 3P
  builds, no file copying or `root_dir` rewrites).
- **`tools/bazel/BUILD.bazel` & `tools/bazel/g3_mode.bzl`**: Defines
  `//tools/bazel:g3_mode`, `//tools/bazel:is_g3_mode`, and the `g3_test`
  transition wrapper.
- **`tools/defaults.bzl`**: Forwards `--ngG3Mode` to `ts_project` and
  `ng_project` when `is_g3_mode` is active.
- **`packages/core/test/acceptance`**: Scopes `acceptance_g3` to
  `**/*_g3_spec.ts` (excluded from 3P `acceptance_lib`) and adds a
  `g3_mode_g3_spec.ts` smoke test.
@JeanMeche JeanMeche mentioned this pull request Nov 26, 2026
build: add Bazel g3_mode flag and acceptance_g3 test target #72216
Open
@JeanMeche
JeanMeche force-pushed the fix/53894-inject-generic-type branch from 9628aad to e844a27 Compare November 26, 2026 00:32
@angular-robot angular-robot Bot added the area: build & ci Related the build and CI infrastructure of the project label Nov 26, 2026
@JeanMeche
refactor(core): add g3-only overloads to preserve generic token types
917340b
In TypeScript, instantiation expressions on class constructors specialize
construct signatures (`new`) while leaving `prototype` unspecialized.
Because `ProviderToken<T>` includes `AbstractType<T>` (`{prototype: T}`),
passing specialized generic tokens to `inject()`, `Injector.get()`,
`EnvironmentInjector.get()`, `TestBed.inject()`, or signal queries caused
TypeScript to infer `T` from `prototype`, widening the return type to
`any`.

Tightening this inference is a breaking change for code relying on the
implicit `any` return type (for example,
`s: GenericService<Foo> = inject(GenericService)`). To roll this out
safely in two steps, this change adds the stricter overloads behind
`g3-only` markers so they take effect internally first, leaving external
(3P) types unchanged until the v23 breaking-change window opens. Each
overload pairs `abstract new (...args: any[]) => T` (preserving
unspecialized `ElementRef<any>`) with `abstract new (...args: never[]) => T`
(matching classes with required constructor parameters such as
`ElementRef<HTMLButtonElement>`, `TemplateRef<Ctx>`, and services using
constructor DI under `strictFunctionTypes`):

- `inject()`, `Injector.get()`, `EnvironmentInjector.get()`, and
  `TestBed.inject()`: add paired `any[]`/`never[]` constructor overloads.
- Signal queries (`viewChild`, `viewChildren`, `contentChild`,
  `contentChildren`): add paired `any[]`/`never[]` overloads for locators
  and `read` options.
- `injector_discovery_utils.ts`: handle `NgModuleRef<unknown>` inferred
  from `injector.get(viewEngine_NgModuleRef, ...)`.
- Tests: keep 3P tests preserving current `any` inference in `di_spec.ts`
  and add active g3-mode specs in `di_g3_spec.ts` and
  `signal_queries_g3_spec.ts`.

Related to angular#53894 and angular#48126 (will be closed once enabled in 3P for v23).
@JeanMeche
JeanMeche force-pushed the fix/53894-inject-generic-type branch from e844a27 to 917340b Compare November 26, 2026 00:45
@JeanMeche
JeanMeche requested review from atscott and removed request for kirjs and thePunderWoman November 26, 2026 00:48
@pullapprove
pullapprove Bot requested a review from josephperrott November 26, 2026 00:48
@JeanMeche JeanMeche added requires: TGP This PR requires a passing TGP before merging is allowed state: blocked on G3 cleanup This change requires a G3 cleanup labels Nov 26, 2026
@JeanMeche
JeanMeche marked this pull request as draft November 26, 2026 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@atscott atscott Awaiting requested review from atscott
@josephperrott josephperrott Awaiting requested review from josephperrott

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

area: build & ci Related the build and CI infrastructure of the project area: core Issues related to the framework runtime requires: TGP This PR requires a passing TGP before merging is allowed state: blocked on G3 cleanup This change requires a G3 cleanup

Projects

None yet

Milestone

Backlog

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@JeanMeche @SkyZeroZx
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.