1

I have several mistakes in my code. I'm use Angular 2 + TSLint:

constructor(http: Http) {
    this.http = http;
--> let currentUser = JSON.parse(localStorage.getItem("currentUser"));
    this.token = currentUser && currentUser.token;
}

In currentUser i have this error: message: 'expected variable-declaration: 'currentUser' to have a typedef;

public loginC (username: string, password: string): Observable<boolean> {
    return this.http.post( authURL + loginURL,
                     --> JSON.stringify({ password: password, username: username }))
    .map((response: Response) => {
        let token: string = response.json() && response.json().token;
        if (token) {
          this.token = token;
      --> localStorage.setItem("currentUser", JSON.stringify({token: token, username: username}));
            return true;
        } else {
            return false;
        }
    });
}

And in password: password, username: username this: message: 'Expected property shorthand in object literal. I really understand this. And finalli i can write a simple model: any {};

export class LoginComponent {
--> public model: any = {};
public loading: boolean = false;
public error: string = "";
constructor (private router: Router, private authenticationService: ServerDataComponent) {
    // 
}

public login(): void {
    this.loading = true;
    this.authenticationService.loginC(this.model.username, this.model.password)
        .subscribe(result => {
           --> if (result === true) {
                this.router.navigate(["/table_per"]);
            } else {
                this.error = "Введен неверный логин и/или пароль";
                this.loading = false;
            }
        });
}

For any - Type declaration of 'any' is forbidden;

Ror result - expected arrow-parameter: 'result' to have a typedef

1 Answer 1

2

For expected variable-declaration: 'currentUser' to have a typedef you can define an interface for your custom type.

export interface User {
  token: string;
}

And use it to set the type.

let currentUser: User = JSON.parse(localStorage.getItem("currentUser"));

For Expected property shorthand in object literal you can use the shorthand syntax when the key name matches the name of the variable.

JSON.stringify({token, username})

For Type declaration of 'any' is forbidden you can try to change the type to Object. If not you'll need to declare another interface for your model.

public model: Object = {};

For expected arrow-parameter: 'result' to have a typedef you need to set the type of the parameter.

.subscribe((result: boolean) => {
Sign up to request clarification or add additional context in comments.

4 Comments

i add a Object, and now, Property token does not exist of type Object
add this in constructor?
No, outside of your class, you can either create a new file user.interface.ts and import it or have it on the same file.
thanks, it works, and i have two more mistakes, i upadate the code now

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.