DEV Community

Discussion on: Why using JS classes instead of Typescript interfaces?

Collapse
 
ricobrase profile image
Rico Brase

Usually in Typescript, interfaces are used to describe the structure of an object, just as in your example.

classes usually contain actual behaviour code in them. In default configuration (strict: true), Typescript will complain about

class Post {
  title: string;
}

with the following error message:

Property 'title' has no initializer and is not definitely assigned in the constructor.


Additionally, interfaces are purely used in type-checking, since they don't exist in Javascript - they don't get any compilation output! If you create a post.ts file and fill it with your example code from above, your compiled post.js file will only contain

class Post {
}