Skip to main content

Command Palette

Search for a command to run...

How to setup a typescript environment for your codewars locally

Updated
2 min read
How to setup a typescript environment for your codewars locally

Setting up Typescript

Note: I have a preference to not install typescript globally and only per project. This setup utilizes vscode.

  1. Create your project folder and navigate into it.

  2. Create a sub folder "src", in your project folder, in which you will place your code. Selection_1381.png

  3. in the integrated CLI, from your project folder, run the following command

    yarn add -D typescript ts-node
    
  4. Run the following command in the CLI to initialize the typescript setup

    npx tsc --init
    
  5. configure the generated tsconfig.json to your liking. Here are my settings for example.

    {
     "compilerOptions": {
         "target": "es2016" ,
         "experimentalDecorators": true ,
         "module": "CommonJS" ,
         "rootDir": "./src" ,
         "moduleResolution": "node" ,
         "sourceMap": true ,
         "outDir": "./dist" ,
         "removeComments": true ,
         "noEmitOnError": true ,
         "esModuleInterop": true ,
         "forceConsistentCasingInFileNames": true ,
         "strict": true ,
         "noUnusedLocals": true ,
         "noUnusedParameters": true ,
         "noImplicitReturns": true ,
         "noImplicitOverride": true ,
         "allowUnreachableCode": false ,
         "skipLibCheck": true 
     }
    }
    
  6. Set up debugging by opening debugger and create a new "launch.json" file.

    "preLaunchTask": "tsc: build - tsconfig.json",
    

    Add the above line, above the "outFiles" line in "launch.json". Selection_1382.png

  7. Finally to run your code, create a new typescript file, for example "myfile.ts", and from the CLI

    npx ts-node ./src/myfile.ts
    

    The output to your script will be in the terminal.

Optional: Setting up testing

  1. Install mocha and chai
    yarn add -D chai mocha @types/chai @types/mocha
    
  2. Modify "package.json" add "test": "mocha --require ts-node/register src/**/*.ts" to your scripts key. Selection_1384.png

  3. For simplicity I write the solution and test in the same file. Note too that I name the test similarly to the solution being solved. This will come in handy for running single test. Selection_1385.png

  4. Run the test yarn test --grep "Reverse words"
    I use the name of the test to run ONLY that single test. As we probably do not want to run all test.

All done.

Additionally you can set up a git repo to upload your work to a github or your remote git repository of choice.