-
Notifications
You must be signed in to change notification settings - Fork 887
no-unnecessary-type-assertion
false positive
#3540
Comments
can you also provide the |
Sure, here's my entire tsconfig:
|
I can confirm that this bug is still present on master with typescript 2.6.1 Some explanation what's happening: You use this method: interface ArrayConstructor {
from<T, U = T>(arrayLike: ArrayLike<T>, mapfn?: (v: T, k: number) => U, thisArg?: any): U[];
} If you don't provide an argument for const elements = <HTMLElement[]> Array.from(document.querySelectorAll('balls')); // U is HTMLElement You get the same result (and no error from this rule) if you just use a type annotation: const elements: HTMLElement[] = Array.from(document.querySelectorAll('balls')); // U is HTMLElement You can even do other crazy stuff that is probably not intended: const elements: string[] = Array.from(document.querySelectorAll('balls')); // U is string - there's no compile error To summarize: I think this should be fixed upstream in TypeScript. There's no good solution to fix this in TSLint. |
Interesting, thanks for the explanation. The type annotation form is actually what I tried first (before even running into this bug), but changed to a cast because vscode showed a compile error. I'm not 100% sure whether I had vscode configured to use 2.6.1 or 2.4.2, but does that compile for you with 2.6.1? |
I was about to open an issue in the TypeScript repo when I tried typescript@next. Note that it only fixes this function by adding another overload. The underlying inference logic that caused the bug is still there. |
Awesome, sounds good. |
Also: declare type RGB = [number, number, number]
function rgbHex(rgb: RGB) { ... }
let rgb = [1, 2,3]
rgbHex(rgb.map(x => x++) as any) The casting is needed above because On the other hand, |
@brandyscarney says: > here are the ones I ran into issues with in the framework: > > * `no-unnecessary-type-assertion` is throwing errors when we cast as an HTMLElement - which is necessary because otherwise it queries as an `Element` and throws errors that the properties don’t exist, this is a bug in TS: palantir/tslint#3540 > * `prefer-for-of` is throwing errors on node lists which is also a bug: palantir/tslint#2927 > * `no-conditional-assignment` is used in DOM controller
I have a similar problem, but the workaround does not work for me. I am using a Reactive Form in Angular, with a getter that returns a control in that form: get email (): AbstractControl { // 1
return this.form.get('email') // 2
} Line 1 declares a non-null return type, but the method called on line 2 returns a nullable type. As shown, tsc reports:
The fix is to annotate with get email (): AbstractControl {
return this.form.get('email')!
} Now, tsc no longer complains, but with
Trying to assign to a temporary variable with a type declaration before returning does not help: get email (): AbstractControl {
const tmp: AbstractControl = this.form.get('email')!
return tmp
} Will this be fixed? I'll sadly have to disable |
I'm experiencing a problem really similar to the one of @thejohnfreeman, where a variable is highlighted by VSCode as possibly undefined (I know it's not, by design) but when i put a |
Disable no-uncessary-type-assertion due to palantir/tslint#3540
Disable no-uncessary-type-assertion due to palantir/tslint#3540
☠️ TSLint's time has come! ☠️ TSLint is no longer accepting most feature requests per #4534. See typescript-eslint.io for the new, shiny way to lint your TypeScript code with ESLint. ✨ It was a pleasure open sourcing with you all! |
🤖 Beep boop! 👉 TSLint is deprecated 👈 (#4534) and you should switch to typescript-eslint! 🤖 🔒 This issue is being locked to prevent further unnecessary discussions. Thank you! 👋 |
Bug Report
TypeScript code being linted
with
tslint.json
configuration:Actual behavior
ERROR: ... This assertion is unnecessary since it does not change the type of the expression.
This looks like #3505, but is actually a separate issue since it was reproduced without any other rules enabled.
Expected behavior
No error. The type of
elements
without the cast would have actually beenElement[]
, breaking the following line that relies on a property ofHTMLElement
.The text was updated successfully, but these errors were encountered: