N
N
Nexta_Live2022-04-11 00:44:06
JavaScript
Nexta_Live, 2022-04-11 00:44:06

How to validate data types in realtime?

Hello, in addition to https://jsonplaceholder.typicode.com/ in the world there is https://breakingbadapi.com/documentation
(Almost the same, but with obviously made mistakes)
the point is that there is a certain field that should be "intager" according to the type documentation but in real life there are still options where a space like is added And this is already parsed into a string, in fact I have an interface like
"season": "1",
"season": " 1",

export interface episode {
  season: number;
}

Can I make a validator call parseInt when the field does not match the type from the interface, but in realtime...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Inviz Custos, 2022-04-11
@Nexta_Live

It is for type validation that you can try this library
https://www.npmjs.com/package/typescript-is
PS

Can I make a validator call parseInt when the field does not match the type from the interface, but in realtime...

What for? The caller must bring the data into conformity with the contract.
And if you really want to transfer strings so much, then bring the interface to the appropriate form:
export interface episode {
  season: number | string;
}

And then you yourself already check what type season has and perform the necessary actions.
Otherwise, what's the point of using TS if you break your own rules.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question