-
Notifications
You must be signed in to change notification settings - Fork 26.2k
New TestBed.get shows deprecation warning for abstract types and everywhere if using a strict tsconfig #29905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
For abstract types you should cast to
No because |
For example: if you were using The change in More info on Issue #26491 |
@Goodwine Thank you for your answer. Yeah I knew this was the goal and I very much look forward to it ;) My point is:
It would be awesome to not have to cast, because I think it's not a very good developer experience to have to write: import { Type } from '@angular/core';
const http = TestBed.get(HttpTestingController as Type<HttpTestingController>); |
I realize the new signature has another issue. I had many warnings with abstract classes but I also had others that looked weird and I figured it out. If you have a strict TS config, more specifically if you have @Injectable({
providedIn: 'root'
})
export class UserService {
constructor(private http: HttpClient) {}
} const service: UserService = TestBed.get(UserService); // tslint is unhappy I guess this is because the export declare const Type: FunctionConstructor;
export declare interface Type<T> extends Function {
new (...args: any[]): T;
} (which is OK for bivariance check but not for contravariance check like This is going to be confusing for developers that activated the |
Re: better type for abstract class Re: |
@cexbrayat I tried reproducing the issue and couldn't, I guess that's because the error comes from a tslint check and not from the compiler repro attempt: (in options enable |
@Goodwine Sorry if I wasn't clear. The TS rule failing is still the See repo for a reproduction: https://github.com/cexbrayat/test-bed-get Or you can manually reproduce with:
Replace user.service.ts by: import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class UserService {
constructor(private http: HttpClient) {}
} Change the {
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "esnext",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"strict": true,
"importHelpers": true,
"target": "es2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
]
}
} Then run
|
Thanks for the detailed repro steps, I'll take a look tomorrow and will try to come up with a PR to fix this issue. |
This is - microsoft/TypeScript#24428 I'll see whether I can update |
PR angular#29290 introduced a new `TestBed.get` signature and deprecated the existing one. This raises a lot of TSLint deprecation warnings in projects using a strict TS config (see angular#29905 for context), so we are temporarily removing the `@deprecated` annotation in favor of a plain text warning until we properly fix it. Fixes angular#29905 Fixes FW-1336
PR angular#29290 introduced a new `TestBed.get` signature and deprecated the existing one. This raises a lot of TSLint deprecation warnings in projects using a strict TS config (see angular#29905 for context), so we are temporarily removing the `@deprecated` annotation in favor of a plain text warning until we properly fix it. Fixes angular#29905 Fixes FW-1336
After talking to the team it was decided to temporarily remove the |
Thanks, can you look at my 2 PRs that would fix this? |
PR angular#29290 introduced a new `TestBed.get` signature and deprecated the existing one. This raises a lot of TSLint deprecation warnings in projects using a strict TS config (see angular#29905 for context), so we are temporarily removing the `@deprecated` annotation in favor of a plain text warning until we properly fix it. Fixes angular#29905 Fixes FW-1336
… overload PR angular#29290 introduced a new `TestBed.get` signature and deprecated the existing one. This raises a lot of TSLint deprecation warnings in projects using a strict TS config (see angular#29905 for context), so we are temporarily removing the `@deprecated` annotation in favor of a plain text warning until we properly fix it. Refs angular#29905 Fixes FW-1336
… overload (#30514) PR #29290 introduced a new `TestBed.get` signature and deprecated the existing one. This raises a lot of TSLint deprecation warnings in projects using a strict TS config (see #29905 for context), so we are temporarily removing the `@deprecated` annotation in favor of a plain text warning until we properly fix it. Refs #29905 Fixes FW-1336 PR Close #30514
… overload (#30514) PR #29290 introduced a new `TestBed.get` signature and deprecated the existing one. This raises a lot of TSLint deprecation warnings in projects using a strict TS config (see #29905 for context), so we are temporarily removing the `@deprecated` annotation in favor of a plain text warning until we properly fix it. Refs #29905 Fixes FW-1336 PR Close #30514
Closed by 561e01d |
@kara I think we should keep this open until we properly fix it. The commit removing the For example the new signature still forces us to write: import { Type } from '@angular/core';
const http = TestBed.get(HttpTestingController as Type<HttpTestingController>); which is not ideal. |
How did this get overlooked on the upgrade guides and breaking changes? Unit tests are not second class code. I'm seeing this in the latest release version 8.0.0. Are we awaiting a quick bug-fix release that will allow this to work? Or for 8.0.0 do I just have to use the new |
@jkyoutsey this wasn't meant to be a breaking change, but due to a TSLint bug it ended up being one. angular/packages/core/testing/src/test_bed_common.ts Lines 117 to 126 in a73b8a6
|
…32200) TestBed.get is not type safe, fixing it would be a massive breaking change. The Angular team has proposed replacing it with TestBed.inject and deprecate TestBed.get. Deprecation from TestBed.get will come as a separate commit. Issue angular#26491 Fixes angular#29905 BREAKING CHANGE: Injector.get now accepts abstract classes to return type-safe values. Previous implementation returned `any` through the deprecated implementation. PR Close angular#32200
…32200) TestBed.get is not type safe, fixing it would be a massive breaking change. The Angular team has proposed replacing it with TestBed.inject and deprecate TestBed.get. Deprecation from TestBed.get will come as a separate commit. Issue angular#26491 Fixes angular#29905 BREAKING CHANGE: Injector.get now accepts abstract classes to return type-safe values. Previous implementation returned `any` through the deprecated implementation. PR Close angular#32200
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
🐞 bug report
Affected Package
The issue is caused by package @angular/core/testing
Is this a regression?
Yes, the previous version in which this bug was not present was:
8.0.0-beta.11
Description
8.0.0-beta.12
introduced a newTestBed.get
method and deprecated the existing one (see609024f):
angular/packages/core/testing/src/test_bed.ts
Lines 246 to 256 in 609024f
The new signature should be transparent for users as
const service = TestBed.get(UserService);
works.But the new signature does not work with abstract types, like for example
HttpTestingController
(see https://github.com/angular/angular/blob/master/packages/common/http/testing/src/api.ts#L29) which leads to confusing deprecation warnings.Update: this is even happening on every
TestBed.get
calls if you have a strict tsconfig, see #29905 (comment)🔬 Minimal Reproduction
🔥 Exception or Error
ng lint
shows:🌍 Your Environment
Angular Version:
cc @Goodwine who wrote it and @mhevery who reviewed: I have not followed closely but couldn't
get<T>(token: T | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
be enough?The text was updated successfully, but these errors were encountered: