<< Blog Index

TypeScript package example
February 20, 2023

A lot of configuration can be stuffed in a package.json these days. Here is an example package.json for TypeScript, ESLint and Jest, for creating an npm package or such:

{
  "name": "@mukunda/{{name}}",
  "version": "1.0.0",
  "description": "{{description}}",
  "main": "lib/{{name}}.js",
  "types": "lib/{{name}}.d.ts",
  "scripts": {
    "test": "jest",
    "build": "tsc",
    "lint": "npx eslint .",
    "prepare": "npm run build",
    "prepublishOnly": "npm test && npm run lint",
    "preversion": "npm run lint"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/mukunda-/{{name}}.git"
  },
  "author": {
    "name": "Mukunda Johnson",
    "email": "mukunda@mukunda.com",
    "url": "https://mukunda.com/"
  },
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/mukunda-/{{name}}/issues"
  },
  "homepage": "https://github.com/mukunda-/{{name}}#readme",
  "devDependencies": {
    "@types/jest": "^29.4.0",
    "@typescript-eslint/eslint-plugin": "^5.52.0",
    "@typescript-eslint/parser": "^5.52.0",
    "eslint": "^8.34.0",
    "jest": "^29.4.3",
    "ts-jest": "^29.0.5",
    "typescript": "^4.9.5"
  },
  "files": [
    "lib/**/*"
  ],
  "jest": {
    "transform": {
      "^.+\\.(t|j)sx?$": "ts-jest"
    },
    "testMatch": [
      "**/*.test.ts"
    ],
    "moduleFileExtensions": [
      "ts", "tsx", "js", "jsx", "json", "node"
    ]
  },
  "eslintConfig": {
    "env": {
      "browser": true,
      "es2021": true
    },
    "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
    "plugins": ["@typescript-eslint"],
    "root": true,
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
      "ecmaVersion": "latest",
      "sourceType": "module"
    },
    "rules": {
      "no-unused-vars": "off",
      "@typescript-eslint/semi": "error",
      "@typescript-eslint/no-empty-function": "error"
    }
  }
}

And my accompanying tsonfig.json, which you can't stuff in a package.json yet:

{
  "compilerOptions": {
      "outDir": "./lib",
      "module": "es6",
      "target": "es6",
      "strict": true,
      "declaration": true,
      "moduleResolution": "node",
      "esModuleInterop": true
  },
  "include": ["src/{{name}}.ts"],
  "exclude": ["src/**/*.test.ts"],
}

Obviously you would want to update the library versions and such, but the basic gist is that you write your code in src/{{name}}.ts and then have the conveniences:

# Run tests
npm test

# Compile
npm run build

# You can configure linting rules in package.json
npm run lint

# Use for bumping versions
npm-version

# Publishing the package
npm-publish

Linting/tests is checked for versioning/publishing via prepublishOnly/preversion. TS config is set for modern/ES6.

<< Blog Index