7

I'm getting errors in typescript when writing basic javascript ES2015 functions. e.g. I get the following error for not declaring the type everywhere:

src\app\component.ts(44,18): error TS7006: Parameter 'a' implicitly has an 'any' type.

src\app\component.ts(44,21): error TS7006: Parameter 'b' implicitly has an 'any' type.

src\app\component.ts(45,20): error TS7006: Parameter 'p' implicitly has an 'any' type.

src\app\component.ts(45,23): error TS7006: Parameter 'n' implicitly has an 'any' type.

The 'problem' function is an ES2015 function like this:

this.filters = this.items
  .map(x => x.someProperty)
  .reduce((p,n) => p.concat(n), [])
  .sort((a, b) => {/* sort someway */})
  .reduce((p, n) => {/* do something */}, [])

I like the readabilty of this function and I know it works just fine. If I have to add type everywhere it defeats the purpose of having terser syntax.

I understood that Typescript was a superset of ES2015 but everything I write reports as an error (not warning) unless I specify every single type. It's a bit annoying and for some reason the errors crash my compiler.

That being said. I do like writing new code with strong types and I do like getting warnings. Just some in some functions it would be nice to turn off the checks.

Is there away to declare a function that ignores the rules or must I rewrite all my JavaScript in this way?

3
  • 4
    have you tried setting "noImplicitAny": false in tsconfig compiler options? Commented Apr 1, 2016 at 9:44
  • You have just saved me countless hours! Thanks. Please post answer so I can accept. Commented Apr 1, 2016 at 10:09
  • Is that code calling reduce on an array of arrays with a label property? I bet it's probably taking an array of objects with a label property and reducing them to the same thing (so the last reduce would be unnecessary), but it's the morning here so I'm probably mistaken :) By the way, in case you don't want to set noImplicitAny to false, you can type this.items and the types of the parameters in map, reduce, and sort will be inferred for you and the errors will go away. Commented Apr 1, 2016 at 13:58

1 Answer 1

11

Try setting "noImplicitAny": false in tsconfig compiler options:

{ 
     "compilerOptions": { 
         "noImplicitAny": false
     }
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.