Answer the question
In order to leave comments, you need to log in
How to make friends a type/interface with a class in TS?
The task is to convert the data stored on the back into a class that will hang additional methods for working with data and some additional fields that are needed only as part of the work inside the front, but are not needed on the back.
More or less like this:
type UserType = {
firstname: string
lastname: string
age: number
}
class User {
id: string
firstname: string
lastname: string
age: number
constructor(user: UserType) {
this.id = nanoid()
this.firstname = user.firstname
this.lastname = user.lastname
this.age = user.age
}
someMethod() {
// some work
}
someMethod2() {
// some work 2
}
toObject(): UserType {
const user_object: UserType = {
// blah blah
}
return user_object
}
}
class User implements UserType { // code }
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question