Gordon  Taylor

Gordon Taylor

1624962180

TypeScript: Interfaces or Types. What’s the Difference?

When I first started using TypeScript I was presented with a conundrum. I could declare a type like this:

interface Person {
  firstName: string;
  lastName: string;
}

or like this:

type Person = {
  firstName: string;
  lastName: string;
}

Both were valid, and worked, so it was very confusing. Why have two ways to do the same thing. Then I read briefly into it, and the advice was use interface, not type. So I did, and I thought no more about it.

But that didn’t really tell me what each did, why I should use the interface type, and where that rule may not apply. As I began to use TypeScript more, type began to creep back into my usage, where it was necessary, and over time I’ve gradually understood the purposes of the two keywords, and I’m going to attempt to explain the difference, both in what we can do, and how I logically think about it.

#pburgess #tech #typescript

TypeScript: Interfaces or Types. What’s the Difference?