34
votes

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?

6

6 Answers

52
votes

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

46
votes

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

5
votes

** 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)   
  }
2
votes

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'`
1
votes

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.

0
votes

This did the trick for me. Check how I have used the x variable

   From

     const user = users.find(x => x.username === username && x.password === password);

   To
const user = users.find((x:any) => x.username === username && x.password === password);