Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

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

Closed
CSchulz opened this issue May 10, 2017 · 24 comments

Comments

@CSchulz
Copy link
Contributor

CSchulz commented May 10, 2017

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.

@andy-hanson
Copy link
Contributor

Could you provide a reproducible example?

@CSchulz
Copy link
Contributor Author

CSchulz commented May 11, 2017

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'.

@ajafff
Copy link
Contributor

ajafff commented May 12, 2017

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?

@CSchulz
Copy link
Contributor Author

CSchulz commented May 12, 2017

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.

@CSchulz
Copy link
Contributor Author

CSchulz commented May 14, 2017

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": "^_"
      }
    ]

@andy-hanson
Copy link
Contributor

May be #2649.

@CSchulz
Copy link
Contributor Author

CSchulz commented May 14, 2017

Yes could be possible, I was not sure.

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

@CSchulz
Copy link
Contributor Author

CSchulz commented May 15, 2017

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.

@CSchulz
Copy link
Contributor Author

CSchulz commented May 15, 2017

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

@andy-hanson
Copy link
Contributor

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.

@CSchulz
Copy link
Contributor Author

CSchulz commented May 16, 2017

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

@andy-hanson
Copy link
Contributor

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.

@CSchulz
Copy link
Contributor Author

CSchulz commented May 17, 2017

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?

@andy-hanson
Copy link
Contributor

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

@CSchulz
Copy link
Contributor Author

CSchulz commented May 19, 2017

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

@nicolashenry
Copy link

@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;
}

@CSchulz
Copy link
Contributor Author

CSchulz commented May 22, 2017

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

@nicolashenry
Copy link

@CSchulz I will but it will take some time.

@CSchulz
Copy link
Contributor Author

CSchulz commented Jun 6, 2017

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

@CSchulz
Copy link
Contributor Author

CSchulz commented Jun 13, 2017

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.

@OliverJAsh
Copy link
Contributor

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

@miltonhowe
Copy link

miltonhowe commented Nov 7, 2017

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.

@adidahiya
Copy link
Contributor

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.

@JoshuaKGoldberg
Copy link
Contributor

🤖 Beep boop! 👉 TSLint is deprecated 👈 and you should switch to typescript-eslint! 🤖

🔒 This issue is being locked to prevent further unnecessary discussions. Thank you! 👋

@palantir palantir locked and limited conversation to collaborators Sep 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants