Reputation: 74
Can anybody tell me what i'm doing wrong... i'm trying to use an interface inside a class an initialize it, but i'm getting this error :
Uncaught TypeError: Cannot set property 'name' of undefined
at new User (eval at setTimeout (main.js:493), <anonymous>:4:24)
at eval (eval at setTimeout (main.js:493), <anonymous>:9:14)
at setTimeout (main.js:493)
here is what i'm trying to do:
interface UserInterface {
name: string
email: string
}
class User {
id: string
data: UserInterface
constructor(){
this.data.name = ''
this.data.name = ''
this.id = ''
}
}
const user = new User()
console.log(user.data)
tks people!
Upvotes: 0
Views: 66
Reputation: 14030
You haven't assigned a value to this.data
yet, so it is undefined
.
Instead, assign it like so:
this.data = {
name: '',
email: '',
};
Upvotes: 2