Coder Social home page Coder Social logo

ts-boilerplate's Introduction

TS Boilerplate

This is an opinionated boilerplate for creating a TypeScript project. It includes:

It's opinionated in the sense that it includes a set of tools that I find useful for my own projects. You can use this as a starting point for your own projects, or you can use it as a reference.

This a minimal boilerplate, so it doesn't include any specific framework or library. It's meant to be a starting point.

Usage

This project has been set up so you can click the button at the top of the repository to create a new repository from this template.

Alternatively, you can use the following command to create a new repository from this template using the GitHub CLI:

gh repo create my-new-project --template degu-io/ts-boilerplate --private --clone

or you can use degit to clone the repository:

npx degit degu-io/ts-boilerplate my-new-project

Then set up git and install the dependencies:

cd my-new-project
git init
pnpm install

pnpm

Why use pnpm? It's faster and more efficient than npm or yarn. It uses a single store for all packages, so you don't have to download the same package multiple times for different projects. It also uses hard links to save space on disk.

Basically, it's fast.

TSConfig

There are many options in the tsconfig.json file that you can use to customize the TypeScript compiler. I've included a few options that I find useful, but you can customize it to fit your needs.

This tsconfig.json includes basic options that will lint your code at compile time and in the editor.

I've commented out a few options that I use most often, but that don't really fit a boilerplate. If you are using jsx (e.g. ReactJS) or want to use tsconfig to lint your code, you can uncomment the options that are commented out.

In most cases, projects will use ESLint and Prettier to lint and format the code, so the tsconfig.json file should be kept minimal to avoid conflicts between the two tools. Nothing more annoying than seeing a bunch of red squiggly lines in your editor because ESLint and TypeScript are fighting over the same rules.

ESLint

ESLint can be created by running npm init @eslint/config and running through the prompts.

In this template we have gone for .cjs format as it removes the eslint warning.

You may want to remove node or browser from the env object in the .eslintrc.js file, depending on your project. See other environments here.

I've also added the @typescript-eslint/no-unused-vars ignore pattern ^_.* which means you can mark a variable as unused by prefixing it with an underscore.

Prettier

Following the Prettier install docs we've installed the eslint-config-prettier to prevent conflicts between ESLint and Prettier. There is a prettier plugin for ESLint, but the current advice is to go with eslint-config-prettier. It feels like this is less complexity, i.e. getting ESLint to do less.

Also, we've added husky and lint-staged to run Prettier on staged files. These tools are a godsend ensuring all commits are suitably formatted and linted.

Vitest

The choice to use vitest is due to it's speed, ease of use and similarity to Jest. Generally, it is very quick to set up an run. Though it can sometimes trip up the developer with some of the configuration. Once in place it is a good test framework to work with.

Note the use of globals: true in the vitest.config.ts and "types": ['vitest/global'] in tsconfig.json. This allows global use of basic test methods, such as describe, expect, etc.

There is also a very powerful vitest ui function that gives a report in the browser with some helpful features.

MSW

Who doesn't love a good mock?

Mock Service Worker provides a fairly painless network mocking mechanism that plays nicely with Vitest. It alerts in the test if it finds an unhandled network call.

We have set up a separate directory for the msw server and handlers so that code can be reused across tests.

A note on .editorconfig

EditorConfig is a useful tool (read the docs). However, I have excluded it from this boilerplate as it can conflict with Prettier and ESLint. Usage should be agreed at a team/contributor level.

Roll Your Own

You may feel differently to me in terms of what to use when. Or you might not like the way I've set up this boilerplate. That's fine. You can use this as a reference and create your own boilerplate.

typescript and tsconfig.json

pnpm add --save-dev typescript
npx tsc --init

Running npx tsc --init will create a tsconfig.json file with all but the most basic options commented out. You can then uncomment the options you want to use. Refer to the TypeScript documentation for more information on the options available.

eslint

pnpm add --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin

Running npm init @eslint/config will create an .eslintrc.js file with all but the most basic options commented out. You can then uncomment the options you want to use.

Refer to the ESLint documentation for more information on the options available.

prettier

pnpm add --save-dev prettier eslint-config-prettier
pnpm add --save-dev husky lint-staged

Follow the Prettier install docs to install Prettier and the ESLint plugin. As well as a guide to installing husky and lint-staged.

vitest

pnpm add --save-dev vitest @vitest/ui

See the vitest.config.ts for configuration suggestions.

msw

pnpm add --save-dev msw

Resources

I have found Matt Pocock's videos to be very helpful in understanding the tsconfig.json file and many other aspects of TS.

ts-boilerplate's People

Contributors

rujmah avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.