Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

Does this answer your question? Typescript: TS7006: Parameter 'xxx' implicitly has an 'any' type Liam Apr 13, 2022 at 8:25

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

If you don't want to hide the message, adopt this method stackoverflow.com/a/66352172/3256489 – 4givN Aug 26, 2021 at 23:07 This is a better answer. The other one is simply hiding the message, not fixing what it's complaining about. – TheTC Jul 22, 2019 at 21:09 @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 – Chad Mx May 30, 2020 at 13:27 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. – Serhii Polishchuk May 31, 2020 at 15:14 Just don't forget about () braces when you want to specify a type. Without them, it won't work. – dpatryas May 12, 2021 at 10:25

** 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)   
                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.
– Doj
                Dec 17, 2020 at 2:29

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.

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

    For error Parameter 'dateText' implicitly has an 'any' type.

    As a starting point, read the error literally, and barring other more-proper solutions, use any like below:

    TS ERROR: Plain JS myArray.find( x => x.someValue == comparisonValue)

    TS SUCCESS: Typescript myArray.find( (x:any) => x.someValue == comparisonValue)

    OP's original problem:

    Error Code look like below

    $(() => {
            $('#datepicker').datepicker({
              changeMonth: true,
              changeYear: true,
              yearRange: '1920:2099',
              onSelect: (dateText) => {
                this.dob.setValue(dateText);
    

    Solution error code looks like below

    $(() => {
            $('#datepicker').datepicker({
              changeMonth: true,
              changeYear: true,
              yearRange: '1920:2099',
              onSelect: (dateText:any) => {
                this.dob.setValue(dateText);
    

    If a third party library doesn't provide types, first do an npm search for @types/SOMELIBRARY (replace SOMELIBRARY with the npm name of the module):

    npm search @types/SOMELIBRARY

    If that exists, npm install it:

    npm install @types/SOMELIBRARY

    This is a repository of types defined for projects that don't do it themselves. If type definitions don't exist there, you can define the type yourself:

    // yourmodule.d.ts
    declare module "yourmodule" {
      export default function somefunction(...): T
    

    Typescript should find that and use it to declare the types in yourmodule.

    Look here for more information: https://www.typescriptlang.org/docs/handbook/declaration-files/library-structures.html

    You could simply define all the parts of yourmodule as any here, as a temporary solution, with the ability to easily add more details as you go. If you end up with a pretty nice and usable yourmodule.d.ts, consider adding it to DefinitelyTyped: https://github.com/DefinitelyTyped/DefinitelyTyped This is where the above @types/LIBRARY type definitions come from.

    selector: 'app-info', templateUrl: './info.component.html', styleUrls: ['./info.component.css'] export class InfoComponent implements OnInit { constructor() { } ngOnInit(): void { onNameChange(ngModelObj){ console.log(ngModelObj); enter code here 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. – Community Oct 12, 2021 at 7:17

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.