Skip to content

Instantly share code, notes, and snippets.

@marcusradell
Last active February 13, 2024 08:22
Show Gist options
  • Save marcusradell/7717da3b2737d218c2f2584cd9df36a0 to your computer and use it in GitHub Desktop.
Save marcusradell/7717da3b2737d218c2f2584cd9df36a0 to your computer and use it in GitHub Desktop.

TypeScript intro

How to setup TypeScript and use in a normal JavaScript project for learning purposes.

Preparations

  • mkdir lab-ts
  • cd lab-ts
  • git init
  • touch .gitignore. Add node_modules and dist to the file.
  • npm init --yes

Install TypeScript

  • npm install typescript

Setup TypeScript

  • npx tsc --init

Configure TypeScript

Inside tsconfig.json, set the following:

  • "rootDir": "./src"
  • "outDir": "./dist"

Verify that it works

  • Create a folder src.
  • Create a file src/index.ts.
  • Write a console.log('Hello!'); statement inside the file.
  • npx tsc
  • Check the build output in the build folder.
  • node build/index.js

Jest intro

How to setup Jest in a TypeScript project.

Preparations

Please do the TypeScript intro first.

Install Jest

  • npm install jest @types/jest ts-jest

Setup Jest

  • npx ts-jest config:init

Verify that it works

  • Create a file index.test.ts.
  • Write a test('It works!', () => {expect(1).toEqual(0);}); statement inside the file.
  • npx jest
  • The test should fail.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment