Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Deno.ServeDefaultExport type #24879

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions cli/tsc/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6256,6 +6256,35 @@ declare namespace Deno {
info: ServeHandlerInfo,
) => Response | Promise<Response>;

/** Interface that module run with `deno serve` subcommand must conform to.
*
* To ensure your code is type-checked properly, make sure to add `satisfies Deno.ServeDefaultExport`
* to the `export default { ... }` like so:
*
* ```ts
* export default {
* fetch(req) {
* return new Response("Hello world");
* }
* } satisfies Deno.ServeDefaultExport;
* ```
*
* @category HTTP Server
*/
export interface ServeDefaultExport {
/** A handler for HTTP requests. Consumes a request and returns a response.
*
* If a handler throws, the server calling the handler will assume the impact
* of the error is isolated to the individual request. It will catch the error
* and if necessary will close the underlying connection.
*
* @category HTTP Server
*/
fetch: (
request: Request,
) => Response | Promise<Response>;
}

/** Options which can be set when calling {@linkcode Deno.serve}.
*
* @category HTTP Server
Expand Down
6 changes: 6 additions & 0 deletions tests/specs/serve/type_check/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"args": "serve --check --port 12345 main.ts",
"output": "main.out",
"tempDir": true,
"exitCode": 1
}
5 changes: 5 additions & 0 deletions tests/specs/serve/type_check/main.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Check [WILDCARD]
error: TS2353 [ERROR]: Object literal may only specify known properties, and 'bad' does not exist in type 'ServeDefaultExport'.
bad() {
~~~
at [WILDCARD]main.ts:2:3
4 changes: 4 additions & 0 deletions tests/specs/serve/type_check/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
bad() {
},
} satisfies Deno.ServeDefaultExport;
6 changes: 6 additions & 0 deletions tests/specs/serve/type_check2/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"args": "serve --check --port 12345 main.ts",
"output": "main.out",
"tempDir": true,
"exitCode": 1
}
5 changes: 5 additions & 0 deletions tests/specs/serve/type_check2/main.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Check [WILDCARD]
error: TS2339 [ERROR]: Property 'doesnt_exist' does not exist on type 'Request'.
console.log(request.doesnt_exist);
~~~~~~~~~~~~
at [WILDCARD]main.ts:3:25
6 changes: 6 additions & 0 deletions tests/specs/serve/type_check2/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
fetch(request) {
console.log(request.doesnt_exist);
return new Response("Hello world!");
},
} satisfies Deno.ServeDefaultExport;