-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Types for RequestHandler are Throwing Errors in Typescript #2494
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
Comments
Probably caused by the changes from #2413. |
@george-lim @ignatiusmb maybe you'd be interested in taking a look at this one? |
Hi. At first, I tried it with the latest Svelte / kit.
After that, I changed these versions according to your
But I couldn't reproduce. npm init svelte@next my-app
# √ Which Svelte app template? » SvelteKit demo app
# √ Use TypeScript? ... Yes
# √ Add ESLint for code linting? ... No
# √ Add Prettier for code formatting? ... No
cd my-app
pnpm i
cat <<"EOL" > src/routes/todos/repl.ts
import { api } from './_api'
import type { RequestHandler } from '@sveltejs/kit'
import type { Locals } from '$lib/types'
export const get: RequestHandler<Locals> = async (request) => {
const response = await api(request, `todos/${request.locals.userid}`)
if (response.status === 404) {
return { body: [] }
}
return response
}
EOL
tsc --noEmit Thank you! |
I've just had the time to take a deeper look at this issue, and actually, yes, I couldn't reproduce it either. Took exactly the same steps as @baseballyama (thanks!) but installing with Closing as it doesn't seem to be an issue, at least an apparent one. Will happily reopen if the author or someone can provide a reproducible example. |
I also tried @baseballyama's steps, but could not reproduce. No errors being thrown. |
Root Cause@ignatiusmb I found out the issue. With ESLint, I was going through my IDE's warnings and issues, I noticed this coming from
I also received this for the
So I changed the the code to the following to add a return type and a better type for import type { Request } from '@sveltejs/kit'
import type { Locals } from '$lib/types'
/*
This module is used by the /todos.json and /todos/[uid].json
endpoints to make calls to api.svelte.dev, which stores todos
for each user. The leading underscore indicates that this is
a private module, _not_ an endpoint — visiting /todos/_api
will net you a 404 response.
(The data on the todo app will expire periodically; no
guarantees are made. Don't use it to organise your life.)
*/
const base = 'https://api.svelte.dev'
interface Response {
status: number
headers?: { location: string }
body?: Record<string, unknown>
}
export const api = async (
request: Request<Locals>,
resource: string,
data?: Record<string, unknown>
): Promise<Response> => {
// user must have a cookie set
if (!request.locals.userid) {
return { status: 401 }
}
const res = await fetch(`${base}/${resource}`, {
method: request.method,
headers: {
'content-type': 'application/json'
},
body: data && JSON.stringify(data)
})
// if the request came from a <form> submission, the browser's default
// behaviour is to show the URL corresponding to the form's "action"
// attribute. in those cases, we want to redirect them back to the
// /todos page, rather than showing the response
if (
res.ok &&
request.method !== 'GET' &&
request.headers.accept !== 'application/json'
) {
return {
status: 303,
headers: {
location: '/todos'
}
}
}
return {
status: res.status,
body: await res.json()
}
} Questions
|
Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
I am using the the basic example in SvelteKit and when I run the following command:
I get typescript errors.
Reproduction
Take the following file:
Then run the following command:
I get the following error:
Logs
No response
System Info
Severity
annoyance
Additional Information
No response
The text was updated successfully, but these errors were encountered: