Data typing

Asked

Viewed 127 times

0

all right?

Putting into practice what I learned in angular/Ionic, I came across some doubts:

1- I made an ion-select with a list of states coming from firebase. When I typed the model States, be in doubt if in the model I do export interface or export class . I saw that I can do it both ways, but what a difference and what a result it implies?

2- When I returned to the firebase status list, I used this.af.database.ref("/states") and not this.af.database.list("/states") as it is on the course. I was doubtful when returning from the service. Because the syntax is different and it was not clear to me what type and how this return is. See:

register.

ionViewDidLoad() {
    this.estados = this.estadosService.listar();
}

cadastro.service.ts

listar():any{ // este any deveria ser do tipo Estados, correto?
    return this.af.database.ref('/estados')
        .once('value')
        .then( (resposta) => {
            return resposta.val();
        })
        .catch( (erro) => {
            console.log(erro);
        })
} 

​ If I could not explain well and want to see the whole code to better understand my doubts, follow on github:

Github

1 answer

0

The return of the List() method of your service registration is of the type "any". That is, it is dynamic. This type of method is interesting to be used when we need to describe the type of variables that are not known when we are writing an application. And these values can come from dynamic content.

See more in:

https://www.typescriptlang.org/docs/handbook/basic-types.html

In the case of the List() method, the call return is not typed anywhere. You would need to create a model and map the result to this type before returning to Component. See more on:

https://stackoverflow.com/questions/36911348/angular2-map-data-as-specific-object-type

Browser other questions tagged

You are not signed in. Login or sign up in order to post.