Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 9, 2022 06:03 pm GMT

Create a TypeScript project

Creating a TypeScript project is easy!

Let's get up and running with build and testing scripts!

You will need a few things first:

Create your project folder

Create a folder with your project's name in [[Project Name]].

NOTE: Node.js convention is to use kebab-case. All letters are lower case, and dashes are used for spaces.

Run the following command:

mkdir [[Project Name]]

Open your folder in your editor. For Visual Studio Code, you can run the following shortcut:

code [[Project Name]]

Install Dependencies

Create your package.json, which stores NPM configuration.

NOTE: When prompted, it is HIGHLY recommended to start with version 0.0.0 or 0.0.1.

This implies that the project is in development. Starting with 1.0.0 implies the project is stable.

Run the following command:

npm init

Install TypeScript and Jest as "devDependencies" in package.json. This will also create a package-lock.json file, which stores specific package version information.

Run the following command:

npm i -D typescript jest ts-jest @types/jest

Configure TypeScript

Creates your tsconfig.json file, which stores TypeScript configuration.

Run the following command:

npx tsc --init

Creates your src/index.ts file.

Run the following commands:

mkdir srctouch ./src/index.ts

In tsconfig.json, update the following "compilerOptions". This configures TypeScript to use your source files, and generate all of the necessary declaration and map files.

  "rootDir": "./src",  "declaration": true,  "declarationMap": true,  "sourceMap": true,  "outDir": "./dist",

In package.json, create the following properties. This configures your package to use the TypeScript generated files.

  "main": "dist/index.js",  "types": "dist/index.d.ts",

Configure Scripts

In package.json, create the following "scripts". These are common scripts for building and testing.

  "scripts": {    "test": "jest",    "start": "tsc --watch",    "build": "tsc",    "clean": "tsc --build --clean"    "prepublishOnly": "tsc --build --clean && tsc",  }

In package.json, add the following section. This tells Jest how to find and run your TypeScript tests.

  "jest": {    "preset": "ts-jest",    "testPathIgnorePatterns": [      "<rootDir>/dist/"    ]  }

Configure Git

Create your Git configuration. It is important to check in only source files.

Run the following commands:

git inittouch .gitignore

Add the following to .gitignore. This will prevent checking in TypeScript generated files.

node_modules/dist/

Configure NPM publish

Create your NPM publish configuration. It is important to publish BOTH source files, and TypeScript generated files.

Run the following command:

touch .npmignore

Add the following to .npmignore. This will prevent checking in unnecessary files.

NOTE: Do not include dist/ or src/ in your .npmignore file.

tsconfig.json

Original Link: https://dev.to/sjohnsonaz/create-a-typescript-project-mco

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To