TypeScript and Netlify Functions

Posted on Fri Oct 01 2021
Updated on Mon Jan 15 2024
typescript serverless netlify

This blog out of date. Netlify has first class support for now TypeScript. Make sure to read Announcing native TypeScript support for Netlify Functions

Did you know that Netlify Functions are just using AWS Lambdas behind the scenes?

This means you can use the same type definitions available for aws-lambda for your Netlify functions too. Install the aws-lamda types by running the following.

npm install @types/aws-lambda --save-dev

You only need to import the APIGatewayProxyEvent, APIGatewayProxyCallback types like so.

import { APIGatewayProxyEvent, APIGatewayProxyCallback } from "aws-lambda";

export const handler = async function (
  event: APIGatewayProxyEvent,
  context: any,
  callback: APIGatewayProxyCallback
) {
  // Do some stuff here	
};

Note, there are no type declarations available for context as this includes properties and methods specific to Netlify such as Netlify Identity .

However, having auto completion for event alone makes this hugely useful!

Iā€™m putting together some TypeScript Netlify Functions examples over at this repo. Feel free to give it a star if you find it helpful.