What you’re talking about is called runtime type checking, and it’s not something that TypeScript natively supports. The types you’ve defined vanish when you transpile your TypeScript into JavaScript, and so as the application is running it has no built-in way of validating the types of any incoming data. Type checking in TypeScript is a compile-time only feature.
It’s important to understand this fundamental limitation, because casting your incoming data, as suggested above, only works if you already know with certainty that your data has that “shape”.
When you cast a value, like an object returned from an api request, to a type/interface in TypeScript, you’re simply making a promise (more technically a “contract”) with the TypeScript compiler saying “I know that this incoming data has this shape, take my word for it and trust me”. If you’re wrong however, and the incoming data doesn’t, say, have a property that you defined on your interface (and therefore promised TypeScript it would have), you’re going to have issues at runtime when your program tries to access that nonexistent property. Because, again, your compiled JavaScript that is actually executing has no notion of the types/classes/interfaces defined in your .ts files.
In order to do your due diligence and actually examine your data as it enters your program to make sure there isn’t a mismatch between what you promised the program it would look like and how it actually looks, you need to run some logic over that data to inspect it. For anything more than a trivial interface with one or two values, you’re going to want to use a library, because it gets very complicated very quickly.
Some libraries that help you do that are:
Now these libraries are only useful if you know ahead of time what the shape of the data is that you’re expecting. If you wrote the api that the data is coming from, this is trivial, even more so if you wrote it in TypeScript, because you can simply import the same types. If you wrote the back end in a different (statically typed) language (C#/Go/Java), you can Google LanguageX to TypeScript type converter and find a program that will parse the source code and generate a TypeScript interface.
For instance:
https://marketplace.visualstudio.com/items?itemName=rafaelsalguero.csharp2ts
However, if the source of the data is not under your control and you’re not exactly sure what the shape of it is (but you know it’s complicated), you can use something like one of these programs to parse the JSON response from your api and generate a set of interfaces that describe it (you would do this part once during development, not each time the program runs).
https://jvilk.com/MakeTypes/
http://json2ts.com/
You can then use the generated interfaces to feed into one of those runtime type-checking libraries I mentioned above. However, this too is fragile in that it will break if and when the api decides to change the shape of its data and you’ll have to do this process all over again.
For true certainty regarding the shape of your data coming from the server to your client application, you need to use GraphQL. This is one of the foundational reasons for its existence (but a little beyond the scope of this post). See here for further reading:
For a more in-depth discussion of what I outlined at the beginning of the post, see these articles (or just Google TypeScript runtime type checking):