Typescript, using classes without constructor

You can use class as a type which is the way you’re using it. So, whether Hero is an interface or class it doesn’t matter because you’re using it as a type.

class Hero { id: number; name: string }

or

interface Hero { id: number; name: string }

The following doesn’t concern whether Hero is a class or interface

let hero: Hero = { id: 1, name: 'me' }

Where interface and class differentiate is interface is only for you, it doesn’t get transpiled into javascript. A class does and you cannot new an interface.

Constructor or no Constructor, if you new it then it is an instanceof. Try it again with this

let hero = new Hero();

Your instanceof log will be true because you did create an instance of Hero with the key word new.

Leave a Comment