-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Cannot use interfaces to type endpoint outputs #1997
Comments
I'm not sure I'm enough of a TypeScript expert to know how to fix this, but I agree it's an issue. If you want to send a PR, I'd merge a fix for this |
Possibly related to microsoft/TypeScript#42825? |
This issue seems to date back to 2017 microsoft/TypeScript#15300 |
This can be fixed by changing diff --git a/node_modules/@sveltejs/kit/types/endpoint.d.ts b/node_modules/@sveltejs/kit/types/endpoint.d.ts
index 2687562..1963e8d 100644
--- a/node_modules/@sveltejs/kit/types/endpoint.d.ts
+++ b/node_modules/@sveltejs/kit/types/endpoint.d.ts
@@ -8,7 +8,7 @@ type JSONValue =
| null
| Date
| JSONValue[]
- | { [key: string]: JSONValue };
+ | Record<string, JSONValue>;
type DefaultBody = JSONValue | Uint8Array;
in endpoint..d.ts |
That is not actually a fix, typescript will give an error: |
This is somewhat a limitation from TypeScript itself -- we want any JSON values to be valid, but can't really properly type it and the current Unfortunately, the fix/workaround right now needs to happen on the user side, but it's pretty simple and won't take too long. Pass in any interface that is going to be returned in the endpoint through the interface below, and it should trick TS while still retaining any property inference export type Typify<T> = { [K in keyof T]: Typify<T[K]> };
// for this issue
type GetOutput = Typify<{ message: string }>;
type PostOutput = Typify<{ message: string }>;
type PutOutput = PostOutput; |
UPDATE: |
For anyone still running into this I thought I'd share the pattern I'm using based on @ignatiusmb idea: in // From https://github.com/sveltejs/kit/issues/1997#issuecomment-887614097
type Typify<T> = { [K in keyof T]: Typify<T[K]> }
declare namespace Client {
declare namespace Page {
type UserPage = Typify<_Page.UserPage>
}
}
declare namespace _Page {
interface UserPage {
// ...
}
} This works but it makes intellisense for dealing with |
Describe the bug
When setting up and endpoint with typescript, it is not possible to have interfaces as endpoint outputs
Reproduction
Logs
System Info
Severity
blocking an upgrade
Additional Information
The issue is inside the
JSONValue
type, using it seems to require all the types assigned to it to have string indexes, can be worked around by using the unsafeany
type, which defeats the whole purpose of having a typed endpointThe text was updated successfully, but these errors were encountered: