打字稿类型检查无法处理Http post响应

问题描述 投票:0回答:1

我有这个Http电话:

getSomething(): Observable<MyTypedClass> {
return this.httpClient.post<MyTypedClass>(this._url, httpOptions)
  .pipe(
    catchError(this.formatErrors)
  );

}

MyTypedClass.ts看起来像这样:

export class MyTypedClass {
  code: string;
  state: string;
}

JSON响应(不匹配我的类型类):

{ "code": "INF00001", "test":true }

由于我的JSON响应与MyTypedClass不匹配以用于测试目的。

问题:我是否应该期望HttpClient抛出一些不匹配或解析错误并触发订阅者的错误功能?

angular typescript typechecking angular-httpclient
1个回答
2
投票

TypeScript的类型系统仅适用于编译时。它指导您编写代码的方式,并在运行时擦除。对于与您的类不匹配的JSON数据,您不会在运行时收到任何错误。

© www.soinside.com 2019 - 2024. All rights reserved.