0

I am getting error of initialisation in my component.ts file

My model is

import { Account } from './account.model';

export interface Owner{
    id: string;
    name: string;
    dateOfBirth: string;
    address: string;

    accounts?: Account[];
}

And I am using it in component.ts file as :

export class OwnerDetailsComponent implements OnInit {
  public owner: Owner;
  public errorMessage: string = '';

  constructor(private repository: RepositoryService, private router: Router, 
              private activeRoute: ActivatedRoute) { }

  ngOnInit() {
    this.getOwnerDetails()
  }

  getOwnerDetails = () => {
    let id: string = this.activeRoute.snapshot.params['id'];
    let apiUrl: string = `my API url`;

    this.repository.getData(apiUrl)
    .subscribe(res => {
      this.owner = res as Owner;
    })
  }

}

I am getting error as : Property 'owner' has no initializer and is not definitely assigned in the constructor.

Please note: I don't want to initialise it with undefined

Please suggest how to remove this error.

1 Answer 1

1

Add (!) sign after property:

owner!: Owner;

or you can disable strict typing in tsconfig.json file;

"angularCompilerOptions": {
    //   ...
    "strictPropertyInitialization": false
    //   ...   }
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.