Checking interface type in TypeScript: how to determine if an object matches an interface?
Hello, colleagues! I started learning TypeScript and encountered a problem that seems elementary, but I can't find the right solution. I have an interface:\n\n```typescript\ninterface User {\n name: string;\n age: number;\n email: string;\n}\n```\n\nI receive data from an API as `any` or `unknown`. I need to check if the received object matches the `User` interface before using its fields. In JavaScript, I would write `typeof obj.name === 'string'` and so on, but in TypeScript I want to do it elegantly using the language's capabilities.\n\nI tried writing `if (obj instanceof User)`, but TypeScript complains that `User` is an interface, not a class. I tried using `typeof obj === 'User'` — that doesn't work either. I googled and found mentions of type guards and custom
No answers yet.
Log in to leave an answer