Using Twilio Serverless with TypeScript
The Twilio Serverless runtime currently does not support running TypeScript by itself but you can use the TypeScript compiler to compile your Functions ahead of time.
We published TypeScript definitions for our Serverless Runtime at @twilio-labs/serverless-runtime-types.
You can create a new Twilio Serverless project via npm init or using the serverless plugin for the Twilio CLI. Either way you can opt to create your new project using TypeScript.
To create a new TypeScript based Twilio Serverless project with npm you can run npm init twilio-function with the --typescript option.
npm init twilio-function project-name --typescript
You can also pass the --typescript option to the Twilio CLI serverless plugin's init command
twilio serverless:init project-name --typescript
Both commands will generate a new project with a srcdirectory that contains your TypeScript source files. When you run npm start or npm run deploy the project will automatically be compiled into the dist directory and run or deployed from there.
Start by installing the TypeScript compiler for your project using npm or another Node.js package manager:
1# Using npm:2npm install --save-dev typescript34# Using yarn5yarn add --dev typescript
Afterwards create your TypeScript configuration. You can do this by manually creating a tsconfig.json in your project or by using the TypeScript compilers --init flag.
1# Using npm's npx command2npx tsc --init34# Using yarn5yarn tsc67# Without either8node_modules/.bin/tsc --init
Your resulting tsconfig.json should look something like this if you ignore the comments:
1{2"compilerOptions": {3"target": "es5",4"module": "commonjs",5"strict": true,6"esModuleInterop": true7}8}9
We need to be able to tell TypeScript about the different types related to the Serverless Runtime. These are called TypeScript definitions and the ones for Serverless Runtime are all bundled in the @twilio-labs/serverless-runtime-types module on npm. You'll need to install it as a dependency for your project.
1# Using npm2npm install @twilio-labs/serverless-runtime-types34# Using yarn5yarn add @twilio-labs/serverless-runtime-types
If you want to convert your existing Functions from JavaScript to TypeScript you'll need to:
- Rename your file to end with
.tsinstead of.js - Change from
require()statements toimport - Add import
'@twilio-labs/serverless-runtime-types';to the top of your file - Import additional types necessary from
@twilio-labs/serverless-runtime-types - Use
export const handlerinstead ofexports.handler
This is how an example TypeScript version of a Twilio Function would look like.
1// Imports global types2import '@twilio-labs/serverless-runtime-types';3// Fetches specific types4import {5Context,6ServerlessCallback,7ServerlessFunctionSignature,8} from '@twilio-labs/serverless-runtime-types/types';910export const handler: ServerlessFunctionSignature = function(11context: Context,12event: {},13callback: ServerlessCallback14) {15const twiml = new Twilio.twiml.VoiceResponse();16twiml.say('Hello World!');17callback(null, twiml);18};19
Since Twilio Serverless and the Toolkit don't support TypeScript out of the box yet, we need to run the compiler before the local development or deployment.
1# Use npm2npx tsc34# Use yarn5yarn tsc67# Run the compiler directly8./node_modules/.bin/tsc
Afterwards you can deploy the project or locally run your project.
Right now we are compiling the TypeScript files in a way that results for the JavaScript output files to live side-by-side with the TypeScript files. This is great because that means the Toolkit commands work without any additional arguments.
To move the output somewhere else, set the outDir option of the compilerOptions inside the tsconfig.json file. Afterwards you'll have to call the Toolkit's start and deploy commands with the --functions-folder flag to point again the functions/ directory inside your output directory.
Now that you know how to combine Twilio Serverless and TypeScript, why not check out some other resources to see what you can do with the Twilio Serverless Toolkit?