2

I am currently developing a application with the Angular2, i created a small private object which will have the firstName and the lastName when i try to transpile the file from typescript to javascript i am getting an error stating

error TS2304: Cannot find name 'firstName'

My code is

export class AppComponent {

public ContactDetail = {firstName="xander",lastName ="xmen"};

}

Is their an possible way to solve this solution

Thanks in advance

2 Answers 2

5

You should get a bit more familiar with the TypeScript syntax.
It starts from the JavaScript syntax, so your object should look like this:

ContactDetail = {
  firstName:"xander",
  lastName:"xmen"
}
Sign up to request clarification or add additional context in comments.

Comments

2

To fix errors with your syntax, you can use:

export class AppComponent {
  public ContactDetail: {firstName:string, lastName:string} = {firstName: "xander", lastName:"xmen"};
}

But to make your architecture more flexible, you can do something like this:

export class ContactDetail {
    firstName: string
    lastName: string;
}

export class AppComponent {
    public ContactDetail: ContactDetail
}

// USAGE
var myContact = new AppComponent;
myContact.ContactDetail = {firstName: 'xander', lastName: 'xmen'}

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.