How to init a new class in TS in such a way (example in C# to show what I want):
// ... some code before
return new MyClass { Field
I wanted a solution that would have the following:
Here is the way that I do it:
export class Person {
id!: number;
firstName!: string;
lastName!: string;
getFullName() {
return `${this.firstName} ${this.lastName}`;
}
constructor(data: OnlyData) {
Object.assign(this, data);
}
}
const person = new Person({ id: 5, firstName: "John", lastName: "Doe" });
person.getFullName();
All the properties in the constructor are mandatory and may not be omitted without a compiler error.
It is dependant on the OnlyData that filters out getFullName() out of the required properties and it is defined like so:
// based on : https://medium.com/dailyjs/typescript-create-a-condition-based-subset-types-9d902cea5b8c
type FilterFlags = { [Key in keyof Base]: Base[Key] extends Condition ? never : Key };
type AllowedNames = FilterFlags [keyof Base];
type SubType = Pick >;
type OnlyData = SubType any>;
Current limitations of this way: