Philip7899 Philip7899 - 1 year ago 77
TypeScript Question

typescript constructor won't let me define anything

I am trying to write a basic class in typescript using ionic 2. Here's my code:

import { Component } from '@angular/core';

import { NavController } from 'ionic-angular';

import {FishPage} from '../fish';

selector: 'page-home',
templateUrl: 'home.html'
export class HomePage {

constructor(public navCtrl: NavController) {
this.salary = 7;


The typescript is not compiling. I am getting this error:

error TS2339: Property 'salary' does not exist on type 'HomePage'.

How do I define salary in the constructor?

Answer Source

Class member need to be defined, either in the constructor signature using public/protected/private (as you did with the navCtrl member) or like this:

export class HomePage {
    private salary: number;

    constructor(public navCtrl: NavController) { ... }

Ionic is not a typescript library but a javascript one, so their documentation is javascript and not typescript.
With javascript (when using es6 classes) you don't need to declare the members like you need to in typescript.

When the typescript code in my answer compiles into javascript it looks like this:

var HomePage = (function () {
    function HomePage(navCtrl) {
        this.navCtrl = navCtrl;
    return HomePage;

As you can see, here too there's no trace of the public/private, or the declaration of the members, that's just a typescript thing and it was there from (probably) the first version.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download