0

I have the following TypeScript interface, which is used to map data from a HTTP response:

interface Person {
   firstName: string;
   lastName: string;
   active: boolean;
}

Here's the sample response:

{
       firstName: 'John',
       lastName: 'Brown',
       active: 1;
}

Here's the logic used to store the response into a local variable:

const person: Person = response;

The following provides the same result:

const person = response as Person;

Based on the logic described above, the person object results in the following:

{
       firstName: 'John',
       lastName: 'Brown',
       active: 1;
}

The problem is the the 'active' property is expected to be a boolean, true or false, not an integer.

Any ideas how to address this issue?

Note that checking the value and converting it is not a viable option because the response could be an array of a hundred objects and I would not want to traverse them.

Also, the backend can not be changed to pass true or false in the response. The response must contain a 1 or 0.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Darrell
  • 457
  • 2
  • 6
  • 18
  • https://stackoverflow.com/questions/73247727/how-to-automatically-convert-yyyy-mm-dd-strings-from-http-responses-to-date?noredirect=1#comment129360762_73247727 – jonrsharpe Aug 05 '22 at 18:49
  • `as Person` is just a type assertion, by the way, **not** a cast or conversion. Fortunately for you if you don't want to do either of the right things it'll work anyway, because 1 is truth-y and 0 is false-y. – jonrsharpe Aug 05 '22 at 18:50
  • "the response could be an array of a hundred objects and I would not want to traverse them" Why not? 100 is small, just `arr.map(conversionFunction)` should do it. Either you have to just put up with whatever the data looks like (e.g., change your interface to accept `0 | 1` instead of `boolean`) or you have to convert the data, even if there's lots of data. What am I missing? – jcalz Aug 05 '22 at 19:04
  • Take a look at Typescript Enums not booleans all together but a workaround – Tushar Mistry Aug 05 '22 at 19:28
  • @jonrsharpe that makes sense. But, is there a way to just have true and false, not 1 or 0? – Darrell Aug 06 '22 at 13:16
  • @jcalz I don't want to traverse the data because it is part of a pageable query from the server side. So, the logic could be invoked many times as a user pages through the data. So, I would like to avoid having to loop through 100 rows every time the user goes to another page, just to translate the column, if possible. – Darrell Aug 06 '22 at 13:19
  • 1
    Not without the two options you've ruled out. Either you have to convert the value for each item, or fix the API. – jonrsharpe Aug 06 '22 at 13:24
  • Either you don't translate the column or you do translate the column. If you don't translate the column then you should change your interface to use `0 | 1`. If you do translate the column then you'll have to do it once per row. I am almost certain that doing `!!x` once per row is not going to be a performance problem, and if it is, your app is doing something unreasonable. Are you not already doing calculations once per row? Can you *demonstrate* that there would be a problem if you do that? – jcalz Aug 06 '22 at 14:22
  • Thanks for the input. I would prefer to use the boolean data type, with true and false values. You have provided options for me to explore. @jcalz I will perform some tests to confirm any performance issue and proceed based on those results. – Darrell Aug 06 '22 at 23:16

0 Answers0