npm package: @lightspeed/config-typescript
Description: Common TypeScript configuration for Lightspeed webapps
Latest version: 2.0.3

# `@lightspeed/config-typescript`

[![npm version](https://badge.fury.io/js/%40lightspeed%2Fconfig-typescript.svg)](https://badge.fury.io/js/%40lightspeed%2Fconfig-typescript)

## Introduction

[**TypeScript**](https://www.typescriptlang.org/) configuration in a convenient package for services and libraries.

## Features

- ✨ Enables TypeScript for typechecking alongside Babel (requires [`@lightspeed/babel-preset-web`](https://github.com/lightspeedretail/web-tools/tree/master/packages/babel-preset-web) or [`@lightspeed/babel-preset-node`](https://github.com/lightspeedretail/web-tools/tree/master/packages/babel-preset-node) packages) in your app or library
- 🚀 Emit types for external libraries
- ➡ Absolute path resolving for root `src`:

```js
// From anywhere in your app, this will resolve to `<root>/src/components/MyComponent.(js,ts,jsx,tsx)
import { MyComponent } from 'src/components/MyComponent';
```

## Installation

First, install required dev dependencies:

```sh
yarn add -D typescript @lightspeed/config-typescript
```

### Services

For projects that uses Babel as a transpiler and TypeScript for type-checking.

1. Install and setup [`@lightspeed/babel-preset-web`](https://github.com/lightspeedretail/web-tools/tree/master/packages/babel-preset-web) (or [`@lightspeed/babel-preset-node`](https://github.com/lightspeedretail/web-tools/tree/master/packages/babel-preset-node) if in a Node.js project)

2. Consume the TypeScript configuration by creating a `tsconfig.json` at the root of your project, and define `baseUrl` in `compilerOptions` for path aliasing to work:

```json
{
  "extends": "@lightspeed/config-typescript/tsconfig.service",
  "compilerOptions": {
    "baseUrl": "."
  }
}
```

> Note: Extending `tsconfig.json` from a package in `node_modules` requires dependency `typescript@^3.2.2`.

3. Optionally, extend the configuration as you see fit (but don't forget to keep `baseUrl`)

```json
{
  "extends": "@lightspeed/config-typescript/tsconfig.service",
  "compilerOptions": {
    "baseUrl": ".",
    "allowJs": true
  }
}
```

### Nest.js

For Nest.js backend applications that use TypeScript for transpiling and type-checking. The configuration is stricter than the default generated `tsconfig.json` from Nest.js boilerplate applications, in particular, enabling the `strict` option.

1. Consume the TypeScript configuration by creating a `tsconfig.json` at the root of your project, and define `baseUrl` in `compilerOptions` for path aliasing to work:

```json
{
  "extends": "@lightspeed/config-typescript/tsconfig.nest",
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "./dist"
  }
}
```

### Libraries

For libraries where you want to output types for external consumers, use the following configuration instead (paths may vary depending on your project structure, but those are typical ones):

```json
{
  "extends": "@lightspeed/config-typescript/tsconfig.library",
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "dist"
  },
  "include": ["./src/**/*.ts", "./src/**/*.tsx"],
  "exclude": ["**/*.test.ts", "**/*.test.tsx"]
}
```

You can still extend as mentioned in the services section.