How to fix [deno-ts 2339] in the code for Deno Deploy
Tags: deno, denodeploy, typescript, lsp
Published: 2021-08-28 00:38:56

I am studying Deno Deploy.

This article summarizes how to deal with the [deno-ts 2339] error I encountered while writing code for Deno Deploy.

## Errors that occur

The error is the one displayed in the editor, `[deno-ts 2339] [E] Property 'respondWith' does not exist on type 'Event'`.

As an example, we will use the code from the Hello World page of [the official documentation](https://deno.com/deploy/docs/hello-world): 

```typescript
// server.ts
addEventListener("fetch", (event) => {
  const response = new Response("Hello World!", {
    headers: { "content-type": "text/plain" },
  });
  event.respondWith(response);
});
```

Even with all this code, the error occurs.

![err](https://storage.googleapis.com/zenn-user-upload/b33bb76f2d4fa6d6a78dca1e.png)

There is a warning that "`respondWith` is not defined in `Event`".

It can be ignored by the Deno Deploy environment and `deployctl`, and `deno lint` does not get particularly angry.
In other words, LSP is just giving you a warning, and you don't need to worry about it.
However, it is not very pleasant to keep getting errors during development. To be honest, it bothers me.

## How to deal with it

An issue about it was raised in the deploy_feedback repository.

https://github.com/denoland/deploy_feedback/issues/13

And this issue was mentioned. We can use this method.

https://github.com/denoland/deployctl/issues/24#issuecomment-819272065

First, generate a type definition file.
```
❯ deployctl types > deploy.d.ts
```

Next, add Triple-Slash Directives on the first part in `server.ts`.

https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html

```typescript
/// <reference path="./deploy.d.ts" />

addEventListener("fetch", (event) => {
  const response = new Response("Hello Deno Deploy!", {
    headers: { "content-type": "text/plain" },
  });
  event.respondWith(response);
});
```

This will remove the error.

![clean](https://storage.googleapis.com/zenn-user-upload/2618cee57666ff2c04134e29.png)

But now `deployctl run . /server.ts` fails.

To make it work correctly, you need to add the libs option at runtime.

```
❯ deployctl run --libs="" ./server.ts
```

This completes the error handling and adjusting the command for execution.

## Use task runner
It is tedious to type the options every time.
You can use Velociraptor, a task runner for Deno, to register the commands to be entered each time.

https://velociraptor.run/

Add the following description to velociraptor.yml.

```yaml
scripts:
  dev:
    desc: Starts local server
    cmd: deployctl run --libs="" --watch ./server.ts
```

You can now start a local server with `vr dev`.
```
❯ vr dev
```

## Closure

Personally, I think that more and more Deno development will be written with the assumption that it will run on Deno Deploy.
That's why I included it in my template repository.

https://github.com/kawarimidoll/deno-dev-template

This is a translation of the following article.
Please leave comments if there are any mistakes in the translation.

https://zenn.dev/kawarimidoll/articles/efed8ada433f24