Typescript - The "u" parameter is implicitly of type "any"

I converted this code to the latest Angular 2. authentication.service.ts

What does the code look like?

app/auth/auth.service.ts(30,40): error TS7006: Parameter 'u' implicitly has an 'any' type. // services/auth.service.ts import {Injectable} from '@angular/core'; import {Router} from '@angular/router'; //http://4dev.tech/2016/03/login-screen-and-authentication-with-angular2/ //https://github.com/leonardohjines/angular2-login export class User { constructor( public email: string, public password: string) { } } var users:any = [ new User(' admin@admin.com ','adm9'), new User(' user1@gmail.com ','a23') ]; @Injectable() export class AuthenticationService { constructor( private _router: Router){} logout() { localStorage.removeItem("user"); this._router.navigate(['Login']); } login(user:any){ var authenticatedUser = users.find(u => u.email === user.email); if (authenticatedUser){ localStorage.setItem("user", authenticatedUser); this._router.navigate(['Home']); return true; } return false; } checkCredentials( ){ if (localStorage.getItem("user") === null){ this._router.navigate(['Login']); } } } 
+6
source share
5 answers

You can try using the User type instead of any :

 var users:User[] = [ (...) ]; 

and

 var authenticatedUser = users.find((u:User) => u.email === user.email); 
+9
source

Consider using (u: any) => ...

+7
source

The cause of the problem is a parameter that is not defined correctly, since this case is a variable "u" according to an example:

 var authenticatedUser = users.find(u => u.email === user.email); 

If you do not have a class of service model, you must define "u" in the "string" or other type, which is what you need:

 var authenticatedUser = users.find(u:string => u.email === user.email); 
+2
source

I use the same example that you used.

Instead of applying any parameters, set the parameter to User .

So your login method would be something like this:

login(user: User): boolean { ...

Then remove any link to the any keyword.

0
source

change

Tsconfig.json file

"noImplicitAny": false,

and add

'ng2-formly': 'npm: ng2-formly / bundles / ng2-formly.umd.js',

in systemjs.config.js

-2
source

All Articles