Serverless TypeScript Plugin

2.1.5 · active · verified Sun Apr 19

The `serverless-plugin-typescript` package offers zero-configuration TypeScript support for applications built with the Serverless Framework, particularly targeting AWS Lambda functions. It automatically handles the compilation of TypeScript source files to JavaScript during standard Serverless deployment lifecycle hooks, such as `sls package`, `sls deploy`, and `sls deploy function`. The current stable version is 2.1.5, with the last update in June 2023 addressing minor bug fixes. Major version changes are less frequent, with v2.0.0 released in September 2021, indicating a slower, maintenance-oriented release cadence rather than rapid feature additions. A key differentiator is its 'zero-config' philosophy, allowing developers to integrate TypeScript without needing to set up Webpack, Babel, or other complex build tools, making it an attractive option for simpler Serverless projects. It supports modern ES2015+ features like `async/await` and works well with `sls invoke local` (including a `--watch` mode) and the popular `serverless-offline` plugin for local development and testing.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to enable the plugin and create a basic TypeScript-based AWS Lambda function, including the necessary `serverless.yml`, `handler.ts`, and `tsconfig.json` files. This setup enables automatic compilation and local testing with `serverless-offline`.

/* serverless.yml */
service: my-typescript-service

frameworkVersion: "2 || 3"

provider:
  name: aws
  runtime: nodejs16.x # Or a newer supported Node.js version
  region: us-east-1

plugins:
  - serverless-plugin-typescript
  - serverless-offline # Example for common integration

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: hello
          method: get

/* handler.ts */
import { APIGatewayProxyHandler } from 'aws-lambda';

export const hello: APIGatewayProxyHandler = async (event, context) => {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: 'Go Serverless with TypeScript! Your function executed successfully!',
        input: event,
      },
      null,
      2
    ),
  };
};

/* tsconfig.json */
{
  "compilerOptions": {
    "preserveConstEnums": true,
    "strictNullChecks": true,
    "sourceMap": true,
    "allowJs": true,
    "target": "es5", // Plugin default, cannot be overridden via tsconfig.json for compilation target
    "outDir": ".build", // Plugin default, cannot be overridden
    "moduleResolution": "node",
    "lib": ["es2015"], // Plugin default
    "rootDir": "./", // Plugin default, cannot be overridden
    "esModuleInterop": true,
    "resolveJsonModule": true
  },
  "include": ["**/*"],
  "exclude": ["node_modules", ".build"]
}

view raw JSON →