Tslint: no-unsafe-any: should not complain about function calls which parameters are using any

Created on 10 May 2017  路  24Comments  路  Source: palantir/tslint

Bug Report

  • __TSLint version__: 5.2.0
  • __TypeScript version__: 2.2.2
  • __Running TSLint via__: CLI

TypeScript code being linted

// part of angular
export declare function inject(tokens: any[], fn: Function): () => any;

// my test
it('should return a new shift', inject([ShiftService, MockBackend], (service: ShiftService, backend: MockBackend) => {

with tslint.json configuration:

{
  "no-unsafe-any": true,
}

Actual behavior

Unsafe use of expression of type 'any'.

Expected behavior

Should not complain about the function inject call.

Most helpful comment

I'm also getting extraneous errors when using this rule in combination with no-unused-variables. If I disable that rule, the errors for no-unsafe-any go.

I'm also seeing extraneous errors with no-inferred-object when used in combination with no-unused-variables

All 24 comments

Could you provide a reproducible example?

Here is a repro repo

https://github.com/CSchulz/tslint-no-unsafe-any-showcase

I am not sure about the imports in _test.ts_ which is also violated with unsafe any, but the usage of methods in the _app.component.spec.ts_ should be okay.

ERROR: src/app/app.component.spec.ts[24, 12]: Unsafe use of expression of type 'any'.
ERROR: src/app/app.component.spec.ts[31, 12]: Unsafe use of expression of type 'any'.
ERROR: src/app/app.component.spec.ts[31, 12]: Unsafe use of expression of type 'any'.
ERROR: src/app/app.component.spec.ts[31, 12]: Unsafe use of expression of type 'any'.
ERROR: src/app/app.component.spec.ts[38, 12]: Unsafe use of expression of type 'any'.
ERROR: src/app/app.component.spec.ts[38, 12]: Unsafe use of expression of type 'any'.
ERROR: src/app/app.component.spec.ts[38, 12]: Unsafe use of expression of type 'any'.

I cloned your repo and and tried to reproduce the lint failures.

Ignoring test.ts I got the following:

These are expected failures.

Maybe there is something missing in your repro repo?

I have the feeling that there are some incompatibilities between different extensions for tslint.

I have tested it again in this moment and I got the same violations as @ajafff. I will try to upload a shrinkwrap file to show my depdency tree.

Seems to be a collision with no-unused-variable. You should reproduce it with the following rules:

    "no-unsafe-any": true,
    "no-unused-variable": [
      true,
      "check-parameters",
      {
        "ignore-pattern": "^_"
      }
    ]

May be #2649.

Yes could be possible, I was not sure.

I am still wondering how I got this error with the initial ruleset.

I can avoid the issue with the initial ruleset after reinstalling the dependencies from scratch. I can't tell which dependencies make trouble with each other.

The usage of @Injectable() creates a false-positive, I have updated the repository.

Injectable() is declared to be of type any. @angular/core/src/di/metadata.d.ts defines export declare const Injectable: InjectableDecorator; and InjectableDecorator declares (): any;.
Or you could just hover over the @Injectable() in the IDE and saw it show up as (alias) Injectable(): any.

So every type which is finally any leads to this violation?

Not sure what you mean -- only any is any. Do you mean that Injectable is () => any and not any? But the error isn't for Injectable, it's for Injectable(), which is just any.

I meant everytime any is used somewhere in the declaration like in this case () => any, it triggers a violation?

How can you prevent it? Using some type assertion using with the Injectable() decorator?

You could always help improve the typings and get rid of any. The rule will also ignore anything explicitly cast with x as any.

Would it be possible to define something better f.e. for the Injectable () => Function?

@CSchulz I use this in a .d.ts file if it can help:

import core from '@angular/core';

declare module '@angular/core' {
    export interface InjectableDecorator {
        (): ClassDecorator;
    }
    export const Injectable: InjectableDecorator;

    export interface HostBindingDecorator {
        (hostPropertyName?: string): PropertyDecorator;
    }
    export const HostBinding: HostBindingDecorator;

    export interface HostListenerDecorator {
        (eventName: string, args?: string[]): MethodDecorator;
    }
    export const HostListener: HostListenerDecorator;
}

@nicolashenry Thanks, did you consider to contribute it to angular itself?

@CSchulz I will but it will take some time.

Thanks, do you have some typings also for the provider definitions?

Do I have to name the file special or do I need a different configuration for tslint?

I have tried to configure the folder as types root in the tsconfig.json without any change.

I am using the normal Angular CLI environment.

I'm also getting extraneous errors when using this rule in combination with no-unused-variables. If I disable that rule, the errors for no-unsafe-any go.

I'm also seeing extraneous errors with no-inferred-object when used in combination with no-unused-variables

Has anyone found a solution for this? I tried the d.ts workaround suggested by @nicolashenry but that caused cascading problems. I am using Angular 5.0.0 and want to implement no-unsafe-any but it is showing up on every @injectable decorator.


_edited_

I tried @nicolashenry 's workaround again and looked at the errors it created, and it turned out that it unearthed a whole bunch of cases where a sclerotic import was referring to a non-existent file. I removed those refs and it looks like it works now.

Going to close this in favor of #2649, as that seems to be the root of these weird side-effectful issues around no-unused-variable.

馃 Beep boop! 馃憠 TSLint is deprecated 馃憟 and you should switch to typescript-eslint! 馃

馃敀 This issue is being locked to prevent further unnecessary discussions. Thank you! 馃憢

Was this page helpful?
0 / 5 - 0 ratings

Related issues

denkomanceski picture denkomanceski  路  3Comments

DanielKucal picture DanielKucal  路  3Comments

cateyes99 picture cateyes99  路  3Comments

avanderhoorn picture avanderhoorn  路  3Comments

jacob-robertson picture jacob-robertson  路  3Comments