Mikita Melnikau
Mikita Melnikau

Reputation: 1765

What means "any[]" in js/ts array?

I learning Angular2 now. In lesson i read that i must save datas into "any" array and after this into example was code like this:

import { Component } from '@angular/core';

import { GithubService } from '../../services/github.service';

@Component({
  selector: 'profile',
  template: `<h1>Profile Component</h1>`,
})
export class ProfileComponent  {
  user:any[];

  constructor(private _githubService:GithubService){
    // this._githubService.getUser().subscribe(user => {console.log(user)});
    this._githubService.getUser().subscribe(user => {
       this.user = user;
    });
  }
}

What means user:any[];? I tried to search it in google on github and so on, but found nothing. Don't know event what to read.

Upvotes: 1

Views: 13238

Answers (3)

Jay Dee
Jay Dee

Reputation: 2656

Using any usually indicated lack of a proper type. Therefore I strongly recomend to define an interface User that describes the user object.

Or even, in this case, use some prepared typed API - like https://www.npmjs.com/package/typed-github-api

Upvotes: 0

TinDora
TinDora

Reputation: 390

user: any; means we can store a variables of type string, number, object, boolean, etc.

user: any[]; means user must be an array and the array elements can have type strings, number,s objects, booleans etc.

Upvotes: 2

Wayne Allen
Wayne Allen

Reputation: 1745

The first thing to know is that any[] is only a valid variable declaration in Typescript not in Javascript.

It is a two part declaration - [] is used in Typescript to declare an array of values. The any is used to declare that the entries in the array can be of any type. In contrast, you could specify only one valid type for the array. For example, string[] would declare an array of string objects. The Typescript compiler would then check that you only add string objects to the array.

Upvotes: 8

Related Questions