Kazi
Kazi

Reputation: 1553

How to use class variables in object in typescript?

suppose I have a class in typescript file like below:

export class app {
  public variable1: string;
  public variable2: number;
  public variable3 = "Hello world"
  constructor(count: number) {
      this.variable1 = "something",
      this.variable2 = 1 + count;
   }

}

now in another file, I am exporting this class as:

import { app } from './directory';
let pageApp:app;

now, how can I access app variables here?

Upvotes: 14

Views: 47953

Answers (5)

Jonathan Herrera
Jonathan Herrera

Reputation: 6204

Typically, the term class variable is used for variables which are class members, i.e. variables that live on the class, but not on the instance. That means, they exist before any instance has been constructed.

In TypeScript, these class variables are created with the static keyword. (This is the reason, why they are also called static methods.) In your example, only variable1 could be made a class variable, because the other two variables are instance members (also called instance variables), because they get their value during instantiation - in the constructor method.

In order to make variable1 a class method, make it static, like so:

export class app {
   static variable1 = "Hello, World";

   public variable2: any;
   public variable3: any;

  constructor(count){
      this.variable1 = "something";
      this.variable2 = 1 + count;
   }
}

Now, it can be addressed from the classapp, without any instantiation:

console.log(app.variable1)  // Hello, World

Upvotes: 6

Skandar B.A
Skandar B.A

Reputation: 51

you need to instantiate the class let instance = new App(1)

you can implement a getter inside your class

public getVariable(){
  return variable1;
}

or you can just declare the variables as public

public variable1;

Upvotes: 0

Shraddha Goel
Shraddha Goel

Reputation: 897

You can simply use the methods and variables of class A in File 1 from File 2 by creating it object

Here is a short example for you

File 1: App.ts

export class App {

variable1: string;
variable2: number;


constructor(count){
    this.variable1="something";
    this.variable2= 1 + count;
 }

}

File 2: File2.ts

import {App} from './App';
console.log("Hello ", new App(2).variable1);

It will work fine. Similarly, you can do with the methods of the class too.

Upvotes: 0

Akj
Akj

Reputation: 7231

Reference --> Typescript classes

Let's say you class as:

export class app {
   //you can specify Public, private, and protected modifiers
   // default consider as public so you can access in other class as well.

   public variable1="Hellow world";
   public variable2: any
   public variable3: any;

  constructor(count){
      this.variable1="something",
      this.variable2=1+count;
   }
}

import in another file

import { app } from './directory';
let var_name = new app('pass_count_here')

//Access
var_name.variable1

Upvotes: 5

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 250336

Your definition of the class is syntactically incorrect, no let in classes and semantically incorrect, you need to declare all fields:

// appClass.ts
export class app {
    variable1: string // implicitly public in typescript
    variable2: number
    variable3 = "Hellow world"
    constructor(count : number) {
        this.variable1 = "something";
        this.variable2 = 1 + count;
    }

} 

With regard to usage, the import should be fine, but you need to import the file in which the class resides (not the directory as your imports suggest) and you need to new up the class to create an instance

import { app } from './appClass'; // notice no extension (no .js or .ts)
let pageApp:app = new app(1);
console.log(pageApp.variable2);

Upvotes: 20

Related Questions