From 80e6ba7033af5caf0a42d080f61ab052a1c1db7f Mon Sep 17 00:00:00 2001 From: Thomas Boop <52323235+thboop@users.noreply.github.com> Date: Wed, 22 Jan 2020 11:53:39 -0500 Subject: [PATCH] Update readme with type assertion information (#310) * Update readme with type assertion information * PR updates --- packages/github/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/github/README.md b/packages/github/README.md index bd5f708b..d6cf49b1 100644 --- a/packages/github/README.md +++ b/packages/github/README.md @@ -55,3 +55,20 @@ const newIssue = await octokit.issues.create({ body: 'Hello Universe!' }); ``` + +## Webhook payload typescript definitions + +The npm module `@octokit/webhooks` provides type definitions for the response payloads. You can cast the payload to these types for better type information. + +First, install the npm module `npm install @octokit/webhooks` + +Then, assert the type based on the eventName +```ts +import * as core from '@actions/core' +import * as github from '@actions/github' +import * as Webhooks from '@octokit/webhooks' +if (github.context.eventName === 'push') { + const pushPayload = github.context.payload as Webhooks.WebhookPayloadPush + core.info(`The head commit is: ${pushPayload.head}`) +} +```