Here is MyClass:
export class MyClass {
one: string;
two: string;
constructor(init?: Partial<MyClass>) {
if (init) {
Object.assign(this, init);
} else {
this.one = 'first';
this.two = 'second';
}
}
getOneAndTwo(): string {
return `${this.one} and ${this.two}!`;
}
}
Here are three ways to instantiate MyClass:
import { MyClass } from './models/my-class';
let mine = new MyClass();
console.log(mine.getOneAndTwo());
mine = new MyClass({
one: 'One',
two: 'Two'
});
console.log(mine.getOneAndTwo());
mine = {
one: 'Three',
two: 'Four'
} as MyClass;
console.log(mine.getOneAndTwo());
The last call to getOneAndTwo() will throw an error that starts off like this:
TypeError: mine.getOneAndTwo is not a function
at Object.<anonymous>
I assume that the TypeScript compiler allows this to compile because it assumes I am treating a TypeScript class like a TypeScript interface. Is there any way I can throw a warning or an error to prevent this assumption from happening?