Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

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 build 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": "./build"

Set "strict": false if you want to be able to write normal JavaScript without types. This should not be done in a real project!

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
You can’t perform that action at this time.