In JavaScript, the number of primitive data types is limited (check out this link). This means that almost everything in JS is treated as an object.
Unlike many other programming languages, JavaScript does not actually have traditional classes. Instead, it uses prototypical inheritance to achieve similar functionalities.
It's important to note that the concept of an interface
only exists in TypeScript and not in plain JavaScript. As mentioned earlier, TypeScript code ultimately gets transpiled into JavaScript at runtime.
While classes may be necessary in certain scenarios in TypeScript (such as when implementing builder patterns in libraries like zod
or trpc
), it's recommended to simply use a type when no methods need to be defined.
I've created a sample on the TS Playground to showcase different ways of declaring and using objects in TypeScript. Make sure to examine the generated JavaScript (.js
) and type declaration files (.d.ts
) to gain a better understanding of TypeScript's behavior.