parameter implicitly has an 'any' type

115,746

Solution 1

First, to make typescript tolerate parameters without declaring their type, edit the tsconfig.json

// disable this rule:
// "strict": true,

// enable this rule:
"noImplicitAny": false

Second, install the tslint npm package as a prerequisite for the tslint vs code extension

npm install -g tslint

Third, install the tslint vs code extension

Solution 2

Specify the type: (callback:any) => { } for example.

Solution 3

** I wouldn't want to modify the config file and dumb down typescript !!

This code was throwing me error:

  onDelete(todo) {
    console.log('delete')
    this.deleteTodo.emit(todo)   
  }

I fixed mine with adding an "any" type:

  onDelete(todo: any) {
    console.log('delete')
  this.deleteTodo.emit(todo)   
  }

Solution 4

I ended up here with the following error. This is the first search result on Bing, so, putting my solution if it helps someone.

Parameter 'onPerfEntry' implicitly has an 'any' type. TS7006

I solved it like this.

Before

const reportWebVitals = onPerfEntry  => {

After

const reportWebVitals = (onPerfEntry : any) => {

I understand its a simple thing but for a beginner like myself, this took a while for me to figure out.

Solution 5

For those who :

  • have this issue when importing js files like import mymodule from '@/path/to/mymodule.js'
  • and don't want to turn noImplicitAny false

you can:

  • create a file named index.d.ts
  • put there something like
declare module '@/path/to/mymodule.js'
// or using wild card *, like `declare module '@/store/*.js'`
Share:
115,746
Ronin
Author by

Ronin

Updated on July 09, 2022

Comments

  • Ronin
    Ronin about 2 years

    I'm using visual studio code for a typescript project, where I use some 3rd party npm js libraries. Some of them don't provide any ts types (types.d.ts file), so whenever I use parameters or variables without specifying their type, vs code's linting shows this error: parameter implicitly has an 'any' type. Also, ts wouldn't compile.

    How can I prevent this from happening?

  • TheTC
    TheTC almost 5 years
    This is a better answer. The other one is simply hiding the message, not fixing what it's complaining about.
  • Srijan Chaudhary
    Srijan Chaudhary over 4 years
    If you dont have tsconfig.js file do the same changes in your tsconfig.json file.
  • Ronin
    Ronin over 4 years
    @SrijanChaudhary thanks for pointing out, this was actually a typo which is now fixed
  • MHS
    MHS over 4 years
    It is only hiding the message not fix the problem!
  • Serhii Polishchuk
    Serhii Polishchuk about 4 years
    Use any type just to fix compile time error is a bad practice.
  • Chad Mx
    Chad Mx about 4 years
    @SerhiiPolishchuk It's not bad practice. Your reaction is that of anyone who is uncomfortable with Dynamically typed languages, and that's ok. This is familiar syntax you will find in any language that has a form of Duck Typing. "any" works and is acceptable because there is no real structure to the expected object. See docs
  • Serhii Polishchuk
    Serhii Polishchuk about 4 years
    I'm pretty comfortable with strict type hinting and dynamic as well, because I work with php from version 5.2. I think It's worth to mention for people who don't know yet why they should specify type. Please, add it to your answer to make this world a little more safe. People should know the truth - specify type for arguments can safe time and money - use it if you know exact type. Very limited cases needs <any> type.
  • Doj
    Doj over 3 years
    Though we thank you for your answer, it would be better if it provided additional value on top of the other answers. In this case, your answer does not provide additional value, since LLL already posted that solution. If a previous answer was helpful to you, you should vote it up once you have enough reputation.
  • dpatryas
    dpatryas about 3 years
    Just don't forget about () braces when you want to specify a type. Without them, it won't work.
  • 4givN
    4givN almost 3 years
    If you don't want to hide the message, adopt this method stackoverflow.com/a/66352172/3256489
  • sardor khaydarov
    sardor khaydarov over 2 years
    ....component.ts
  • Admin
    Admin over 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.