You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/Users/logan/projects/ldam.co.za/frontend/src/routes/blog/index.ts:7:14
Error: Type '() => { body: { posts: BlogMetadata[]; }; }' is not assignable to type 'RequestHandler<Body>'.
Type '{ body: { posts: BlogMetadata[]; }; }' is not assignable to type 'MaybePromise<Either<EndpointOutput<Uint8Array> | EndpointOutput<string> | EndpointOutput<number> | EndpointOutput<false> | ... 5 more ... | EndpointOutput<...>, Fallthrough>>'.
Type '{ body: { posts: BlogMetadata[]; }; }' is not assignable to type 'Only<Fallthrough, EndpointOutput<Uint8Array> | EndpointOutput<string> | EndpointOutput<number> | ... 6 more ... | EndpointOutput<...>>'.
Property 'fallthrough' is missing in type '{ body: { posts: BlogMetadata[]; }; }' but required in type '{ fallthrough: true; }'.
export const get: RequestHandler = function () {
const posts = fs
That led me on a short goose chase through the typings to try figure out why I needed to specify a fallthrough.
type Body = JSONValue | Uint8Array | ReadableStream | import('stream').Readable;
The problem comes to how JSONValue is defined:
export type JSONObject = { [key: string]: JSONValue };
export type JSONValue = string | number | boolean | null | ToJSON | JSONValue[] | JSONObject;
The problem in my case stems from my return type not implementing an index signature, which I only discovered after explicitly trying to make a EndpointOutput object myself:
/Users/logan/projects/ldam.co.za/frontend/src/routes/blog/index.ts:18:3
Error: Type '{ posts: BlogMetadata[]; }' is not assignable to type 'Body'.
Types of property 'posts' are incompatible.
Type 'BlogMetadata[]' is not assignable to type 'JSONValue'.
Type 'BlogMetadata[]' is not assignable to type 'JSONValue[]'.
Type 'BlogMetadata' is not assignable to type 'JSONValue'.
Type 'BlogMetadata' is not assignable to type 'JSONObject'.
Index signature for type 'string' is missing in type 'BlogMetadata'.
const result: EndpointOutput = {
body: {
posts
The example in the docs kind of makes it look like you can return any object you like:
/** @type {import('@sveltejs/kit').RequestHandler} */
export async function get({ params }) {
// `params.id` comes from [id].js
const item = await db.get(params.id);
if (item) {
return {
body: { item }
};
}
return {
status: 404
};
}
item in this case looks like any plain old javascript object, much like my object(array) is.
Either the docs need to specify what constraints the returned object must satisfy, or the type of JSONObject is wrong (especially since index signatures are just a Typescript thing). Indeed, ignoring typescript errors here and running the project, it works fine, so I'm pretty sure the JSONObject constraint is too strict.
If JSONObject is instead defined like so:
export type JSONObject = Record<string, unknown>;
This works fine, but perhaps the index signature approach was taken for a reason.
Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
I got excited about shadow endpoints and just tried to migrate one of my endpoints.
It seemed simple enough, this is what I started with:
(basically verbatim from the sveltekit blog demo)
From the docs, it looks like I can just return
posts
directly in body, and thenexport let posts
in my .svelte file. So that's what I tried.Typescript is unhappy with this:
That led me on a short goose chase through the typings to try figure out why I needed to specify a fallthrough.
EndpointOutput
looks like this:And body is defined like so:
The problem comes to how
JSONValue
is defined:The problem in my case stems from my return type not implementing an index signature, which I only discovered after explicitly trying to make a
EndpointOutput
object myself:The example in the docs kind of makes it look like you can return any object you like:
item
in this case looks like any plain old javascript object, much like my object(array) is.Either the docs need to specify what constraints the returned object must satisfy, or the type of
JSONObject
is wrong (especially since index signatures are just a Typescript thing). Indeed, ignoring typescript errors here and running the project, it works fine, so I'm pretty sure theJSONObject
constraint is too strict.If
JSONObject
is instead defined like so:This works fine, but perhaps the index signature approach was taken for a reason.
Reproduction
See above
Logs
No response
System Info
Severity
annoyance
Additional Information
No response
The text was updated successfully, but these errors were encountered: