0

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!

1 Answer 1

2

You haven't assigned a value to this.data yet, so it is undefined.

Instead, assign it like so:

this.data = {
  name: '',
  email: '',
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.