Straight to the point:
Is there a way to perform auto cast using an interface as a template in Ionic (TypeScript + Angular 5)?
If not, can Lint recognize that an object coming from the server is not within the expected interface contract?
Scenario:
I'm using Ionic (Angular version 5) and a backend in PHP. My problem is when handling the data coming from the server using an interface that I created.
I have two interfaces that work together:
export interface QuestionInterface {
id_question: string,
answers: Array<AnswerInterface>,
question_text: string
}
export interface AnswerInterface {
answer_text: string,
is_correct: boolean
}
Inside the application, everything works perfectly and I get the alerts when I try to use types that are not in the contract. The problem is when the data arrives from the server.
http.get('xxx')
.toPromise()
.then(result=> {
})
The request returns an object that contains, in result.data
, data from a table. However, since the column is_correct
is of type tinyint(1)
, this data arrives at me as "0" or "1".
I did not want to have to do the cast manually, because I think a lot of gambiarra. Can anyone think of any way to perform an "automagic" conversion?
Otherwise, how to make the object coming from the server go through a contract check with the interface?