Consider a scenario where I have a class structured like this:
class MyClass {
a: string
}
Now, let's say I create a variable with the following definition:
let obj: MyClass = { a: 2 }
An error will be triggered in Typescript because 2 is not a string. However, if we introduce a constructor to the MyClass
like so:
class MyClass {
constructor(a: string) {
}
}
In this case, after declaring the variable in the same way, Typescript does not generate any error messages. Is there a method to utilize a constructor and also treat the class as an interface? Your insights are appreciated. Thank you.