TypeScript: What is the difference between interface and type? When to use which?
Hi everyone! I started learning TypeScript and got confused about the difference between `interface` and `type`. They both seem to allow describing object structures, but I don't understand when to use which tool.\n\nLook at my code. I'm trying to describe a user and their address:\n\n```typescript\n// Option with interface\ninterface IUser {\n name: string;\n age: number;\n address: IAddress;\n}\n\ninterface IAddress {\n city: string;\n street: string;\n}\n\n// Option with type\ntype TUser = {\n name: string;\n age: number;\n address: TAddress;\n}\n\ntype TAddress = {\n city: string;\n street: string;\n}\n```\n\nBoth options work, but I've heard there are some fundamental differences. For example, I saw that `type` can do something with unions, while `interface` cannot. And also in some projects
No answers yet.
Log in to leave an answer