I'm bit confused in how to use OOP in typescript.
I'm used to do it with PHP.
1 - Can I use a class as a type without having to fill all attributes values?
2 - Do I really need to create an interface to create class attributes and use it as type in some function?
For example, this is my class:
class User {
protected id: number;
protected name: string;
protected age?: Date;
constructor(id: number, name: string, age: Date) {
this.id = id;
this.name = name;
this.age = age;
getId() return this.id;
getName() return this.name;
setName(value: string) this.name = value;
getAge() return this.age;
setAge(value: Date) this.age = value;
}
And this is my service function:
const test = () => {
const user = new User({id: 1, name: 'Rick' });
}
I tried many ways and all returned some error, this is the main one.
Type '{ id: string; name: string; }' is missing the following properties from type 'User': getId, getName, setName
I know I can do this with interface, but I'm looking for a way to do this without interfaces, if it's possible.