P
P
pandadevelop2021-12-15 18:05:06
typescript
pandadevelop, 2021-12-15 18:05:06

Why doesn't typescript throw an exception for incorrect interface inheritance?

export interface Response {
  success: boolean;
  error: string;
  data: {
    token: string;
    user: object;
  };
}

this.http.post<Response>(`${environment.api}/some-route`, {})
      .subscribe((response: Response) => {
        console.log(response)
      })


A response arrives from the back, which does not correspond to the interface at all.
But here's the question, why doesn't he swear at the wrong response? The console is silent.
I can send anything from the back and the typescript will not swear, although it seemed to me that interfaces are needed for this in order to "typify" the whole thing. At least swear at the required data in Response.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2021-12-15
@pandadevelop

In every first textbook on TS, somewhere at the very beginning it is written that TS does not exist at runtime, so it cannot check anything there.
Have a look at io-ts and similar libraries for runtime validation
https://gcanti.github.io/io-ts/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question