|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import {CommonModule} from '@angular/common'; |
10 | | -import {Attribute, ChangeDetectorRef, Component, ComponentFactoryResolver, ComponentRef, createEnvironmentInjector, Directive, ElementRef, ENVIRONMENT_INITIALIZER, EnvironmentInjector, EventEmitter, forwardRef, Host, HostBinding, ImportedNgModuleProviders, importProvidersFrom, ImportProvidersSource, inject, Inject, Injectable, InjectFlags, InjectionToken, INJECTOR, Injector, Input, LOCALE_ID, ModuleWithProviders, NgModule, NgZone, Optional, Output, Pipe, PipeTransform, Provider, Self, SkipSelf, TemplateRef, Type, ViewChild, ViewContainerRef, ViewEncapsulation, ViewRef, ɵcreateInjector as createInjector, ɵDEFAULT_LOCALE_ID as DEFAULT_LOCALE_ID, ɵINJECTOR_SCOPE} from '@angular/core'; |
| 10 | +import {Attribute, ChangeDetectorRef, Component, ComponentFactoryResolver, ComponentRef, createEnvironmentInjector, Directive, ElementRef, ENVIRONMENT_INITIALIZER, EnvironmentInjector, EventEmitter, forwardRef, Host, HostBinding, ImportedNgModuleProviders, importProvidersFrom, ImportProvidersSource, inject, Inject, Injectable, InjectFlags, InjectionToken, InjectOptions, INJECTOR, Injector, Input, LOCALE_ID, ModuleWithProviders, NgModule, NgZone, Optional, Output, Pipe, PipeTransform, Provider, Self, SkipSelf, TemplateRef, Type, ViewChild, ViewContainerRef, ViewEncapsulation, ViewRef, ɵcreateInjector as createInjector, ɵDEFAULT_LOCALE_ID as DEFAULT_LOCALE_ID, ɵINJECTOR_SCOPE} from '@angular/core'; |
11 | 11 | import {ViewRef as ViewRefInternal} from '@angular/core/src/render3/view_ref'; |
12 | 12 | import {TestBed} from '@angular/core/testing'; |
13 | 13 | import {By} from '@angular/platform-browser'; |
@@ -3467,6 +3467,39 @@ describe('di', () => { |
3467 | 3467 | expect(envInjector.get(TOKEN, undefined, InjectFlags.Optional)).toBeNull(); |
3468 | 3468 | }); |
3469 | 3469 |
|
| 3470 | + it('should include `null` into the result type when the optional flag is used', () => { |
| 3471 | + const TOKEN = new InjectionToken<string>('TOKEN'); |
| 3472 | + |
| 3473 | + @Component({ |
| 3474 | + standalone: true, |
| 3475 | + template: '', |
| 3476 | + }) |
| 3477 | + class TestCmp { |
| 3478 | + nodeInjector = inject(Injector); |
| 3479 | + envInjector = inject(EnvironmentInjector); |
| 3480 | + } |
| 3481 | + |
| 3482 | + const {nodeInjector, envInjector} = TestBed.createComponent(TestCmp).componentInstance; |
| 3483 | + |
| 3484 | + const flags: InjectOptions = {optional: true}; |
| 3485 | + |
| 3486 | + let nodeInjectorResult = nodeInjector.get(TOKEN, undefined, flags); |
| 3487 | + expect(nodeInjectorResult).toBe(null); |
| 3488 | + |
| 3489 | + // Verify that `null` can be a valid value (from typing standpoint), |
| 3490 | + // the line below would fail a type check in case the result doesn't |
| 3491 | + // have `null` in the type. |
| 3492 | + nodeInjectorResult = null; |
| 3493 | + |
| 3494 | + let envInjectorResult = envInjector.get(TOKEN, undefined, flags); |
| 3495 | + expect(envInjectorResult).toBe(null); |
| 3496 | + |
| 3497 | + // Verify that `null` can be a valid value (from typing standpoint), |
| 3498 | + // the line below would fail a type check in case the result doesn't |
| 3499 | + // have `null` in the type. |
| 3500 | + envInjectorResult = null; |
| 3501 | + }); |
| 3502 | + |
3470 | 3503 | it('should be able to use skipSelf injection in NodeInjector', () => { |
3471 | 3504 | const TOKEN = new InjectionToken<string>('TOKEN', { |
3472 | 3505 | providedIn: 'root', |
|
0 commit comments