DEV Community

カワリミ人形
カワリミ人形

Posted on

How to fix [deno-ts 2339] in the code for Deno Deploy

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:

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

Even with all this code, the error occurs.

err

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
Enter fullscreen mode Exit fullscreen mode

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

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

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

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

This will remove the error.

clean

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
Enter fullscreen mode Exit fullscreen mode

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.

scripts:
  dev:
    desc: Starts local server
    cmd: deployctl run --libs="" --watch ./server.ts
Enter fullscreen mode Exit fullscreen mode

You can now start a local server with vr dev.

❯ vr dev
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)