3 ms·
Just to clarify when this is useful - for example, it's great when you receive a typeless JSON object from the outside world, and want to assert its "type" base
by eranation 8y ago
Just to clarify when this is useful - for example, it's great when you receive a typeless JSON object from the outside world, and want to assert its "type" based on structural typing.
If it's not an external object (e.g. created in TypeScript with new), you can just you instanceof / typeof of course.
To be more precise, instanceof, typeof etc are also sort of automatic type guards in TypeScript, and the example in the post is more of a manual type guard (e.g. if you get something from the outside world, without type information, and want to rely on its structure to assert its type)
More on this:
https://basarat.gitbooks.io/typescript/docs/types/typeGuard.html https://basarat.gitbooks.io/typescript/docs/types/typeGuard....