Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
Angular/Mecanografiado - Respuesta de análisis a la interfaz

tengo la siguiente interfaz

 interface MyObj { myString: string; myNumber: string; myDate: Date; } this.http.get<MyObj>(this.getObjectsURL); Result: { "myString": "test", "myNumber": 12, "myDate": "2011-10-05T14:48:00.000Z"}

Si obtengo los datos, el campo myDate es una cadena y no una fecha.

Si transformo MyObj en un JSON y lo analizo nuevamente, también obtengo una cadena de fecha, y no el tipo correcto. Eso es muy malo :-( ¿Es posible analizar typesafe (para solicitudes HTTP y Json Transform, también?) con transformación a los tipos de datos correctos?

Gracias :-)

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puede agregar una tubería para transformar la respuesta cuando se recibe.

Crear como servicio para realizar todas las llamadas api dentro del servicio

 getYourObject() { return this.http.get(this.getObjectsURL) .pipe(map(resp => { resp.myDate = new Date(resp.myDate); return data; })); }

de su componente y agregue el servicio como dependencia entonces

 this.serviceName.getYourObject().subscribe((resp: MyObj)=> console.log(resp));
over 4 years ago · Santiago Trujillo Report

0

Básicamente tiene dos tipos, una representación externa/seriada e interna/no serializada,

 interface MyObj { myString: string; myNumber: string; myDate: Date; } interface MyObjSerialized { myString: string; myNumber: string; myDate: string; }

Puede usar el primer tipo para anotar el resultado de la llamada http.get

 const result: Observable<MyObjSerialized> = this.http.get('http://some.where');

A continuación, debe analizarlo explícitamente y convertirlo al tipo de datos interno:

 const parsed: Observable<MyObj> = result.pipe( // alternative: provide a method reference instead of arrow function // angular brackets <From,To> optional map<MyObjSerialized, MyObj>(res => ({ ...res, myDate: new Date(res.myDate) }) );
over 4 years ago · Santiago Trujillo Report

0

Encontré esta biblioteca: https://github.com/pichillilorenzo/jackson-js Tal vez esta sea mi solución ... ¡Lo probaré!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!