最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript cannot find name error - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question asked Feb 1, 2017 at 23:55 skidskid 9784 gold badges13 silver badges30 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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"
}

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'}
发布评论

评论列表(0)

  1. 暂无评论