'How to add Typescript to Cypress test?

I am struggling to write my Cypress test framework with TypeScript rather than JavaScript. I have been following lots of online tutorials, but haven't been able to get it working properly.

In my Login.ts file below, I'm trying to create a variable, but I get this error Cannot find name 'private'.ts(2304):

These are in my package.json:

"dependencies": {
    "cypress": "^9.5.2",
    "tsify": "^5.0.4",
    "typescript": "^4.6.3"
  },
  "devDependencies": {
    "cypress-cucumber-preprocessor": "^4.3.1"
  },
  "cypress-cucumber-preprocessor": {
    "nonGlobalStepDefinitions": true
  }


Solution 1:[1]

You are not using private in the correct way. This can only be used for member declarations in a class.

you need to use

let accountBalance: number = 0

to use a private member var you would do


class user {
   private _accountBalance : number;

   constructor( ) {

   }

   // Would only include this if you want to be able to set it from externally
   set accountBalance( value : number ) {
       this._accountBalance = value;
   }

   // Would only include this if you wanted to get the balance externally
   get accountBalance() : number {
      return this._accountBalance;
   }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Derek Lawrence