Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/emanuelefavero/typescript-backend-guide
This repo is a syntax guide for using Typescript in the backend
https://github.com/emanuelefavero/typescript-backend-guide
backend express guide nodejs typescript
Last synced: about 1 month ago
JSON representation
This repo is a syntax guide for using Typescript in the backend
- Host: GitHub
- URL: https://github.com/emanuelefavero/typescript-backend-guide
- Owner: emanuelefavero
- Created: 2022-10-19T05:06:37.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-26T18:04:32.000Z (9 months ago)
- Last Synced: 2024-04-27T18:43:39.147Z (9 months ago)
- Topics: backend, express, guide, nodejs, typescript
- Language: TypeScript
- Homepage:
- Size: 83 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Typescript Backend GUIDE
This repo is a guide for using Typescript in the backend
## Configuration
### Install Typescript
```bash
npm i -D typescript ts-node @types/node @types/express nodemon concurrently
```> TIP: `concurrently` is used to run multiple scripts at the same time
- Also, install `express` if you are going to use it:
```bash
npm i express
```### Create tsconfig.json
- Run to create a `tsconfig.json` file:
```bash
npx tsc --init
```- `tsconfig.json` example:
```json
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"rootDir": "src"
},
"lib": ["es2015"]
}
```- `target` : Specify ECMAScript target version: 'es5', 'es6', 'es2016', 'es2020', or 'ESNext'.
- `module` : Specify module code generation: 'commonjs', 'es6', 'es2020', or 'ESNext'.
- `outDir` : Redirect output structure to the directory.
- `rootDir` : Specify the root directory of input files. Use to control the output directory structure with --outDir.
- `strict` : Enable all strict type-checking options.
- `moduleResolution` : Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6).### Create a `src` folder with an `index.ts` file
```bash
mkdir src
touch src/index.ts
```### Add scripts to `package.json`
```json
"scripts": {
"start": "tsc && node dist/index.js",
"build": "tsc",
"dev": "concurrently \"tsc -w\" \"nodemon dist/index.js\""
},
```### Run typescript in development
```bash
npm run dev
```### Run typescript in production
```bash
npm start
```## Syntax Tips
- Usually when using Typescript we use es6 syntax for imports:
```typescript
import express from 'express'
```- Express Request, Response, NextFunction:
```typescript
import { Request, Response, NextFunction } from 'express'app.get('/', (req: Request, res: Response, next: NextFunction) => {
res.send('Hello')
})
```- Declaring .env variables:
```typescript
const mongoDB = process.env.MONGODB_URI || ‘’
const PORT = process.env.PORT ? Number(process.env.PORT) || 3000
```- Mongoose Schemas:
```typescript
import { Schema } from 'mongoose'interface IUser {
name: string
avatar?: string
}const UserSchema = new Schema({
name: {
type: string,
required: true,
},
avatar: String,
})
```- Another approach to declare a schema:
```typescript
import { Schema, InferSchemaType } from 'mongoose'const UserSchema = new Schema({
name: {
type: string,
required: true,
},
avatar: String,
})type IUser = InferSchemaType
```- Mongoose id type (NOTE: its better to let the database add the id for you)
```typescript
import { Type } from 'mongoose'
// Inside an Interface:
_id: Type.ObjectId
```