When instantiating an object I much prefer the following format:
const MyTest = new Test({
title: 'hello';
});
over
const MyTest = new Test('hello');
especially when there are a lot of properties to pass.
I tried to set this up using the following interface and class definitions:
interface ITest {
title: string;
readonly titlePlusCheese: string;
}
class Test implements ITest {
public title: string;
constructor(args: ITest) {
this.title = args.title;
}
get titlePlusCheese(): string {
return `${this.title} CHEESE`;
}
}
However, when calling const MyTest = new Test({ title: 'hello' });
I get the following error:
Property 'titlePlusCheese' is missing in type '{ title: string; }' but required in type 'ITest'.ts(2345)
However, the following works:
interface ITest {
title: string;
readonly titlePlusCheese: string;
}
class Test implements ITest {
public title: string;
constructor(title: string) {
this.title = title;
}
get titlePlusCheese(): string {
return `${this.title} CHEESE`;
}
}
const MyTest = new Test('hello');
which leads me to suspect I'm doing something silly.
Can anyone shed any light on it?