Skip to content

Cannot use nested interfaces to type endpoint output #4926

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

Closed
flut1 opened this issue May 14, 2022 · 2 comments · Fixed by #4939
Closed

Cannot use nested interfaces to type endpoint output #4926

flut1 opened this issue May 14, 2022 · 2 comments · Fixed by #4939
Labels
bug Something isn't working types / typescript
Milestone

Comments

@flut1
Copy link

flut1 commented May 14, 2022

Describe the bug

This is a follow-up on #1997, which was recently fixed in #4897.

The new BodyValidator type seems to work well, but it doesn't seem to work with interfaces that have properties which are themselves interfaces.

Reproduction

If you prefer a repository: check out this file in flut1/sveltekit-endpoint-output-nested-interfaces-repro

import type { RequestHandler } from "@sveltejs/kit";

interface GetOutput {
    item: { message: string }
}

// No TS error
export const get: RequestHandler<any, GetOutput> = async () => {
    return {
        status: 200,
        body: {
            item: { message: 'hello' },
        },
    };
};

interface Item {
    message: string;
}

interface PostOutput {
    item: Item;
}

// TS error: Type '{ message: string; }' is not assignable to type 'never'.ts(2322)
export const post: RequestHandler<any, PostOutput> = async () => {
    return {
        status: 200,
        body: {
            item: { message: 'hello' },
        },
    };
};

Logs

Output of svelte-check:

sveltekit-endpoint-output-nested-interfaces-repro/src/routes/index.ts:26:14
Error: Type '() => Promise<{ status: number; body: { item: { message: string; }; }; }>' is not assignable to type 'RequestHandler<any, PostOutput>'.
  Type 'Promise<{ status: number; body: { item: { message: string; }; }; }>' is not assignable to type 'MaybePromise<RequestHandlerOutput<PostOutput>>'.
    Type 'Promise<{ status: number; body: { item: { message: string; }; }; }>' is not assignable to type 'Promise<RequestHandlerOutput<PostOutput>>'.
      Type '{ status: number; body: { item: { message: string; }; }; }' is not assignable to type 'RequestHandlerOutput<PostOutput>'.
        The types of 'body.item' are incompatible between these types.
          Type '{ message: string; }' is not assignable to type 'never'. 
export const post: RequestHandler<any, PostOutput> = async () => {

System Info

System:
  OS: macOS 12.3.1
  CPU: (8) arm64 Apple M1
  Memory: 111.95 MB / 8.00 GB
  Shell: 5.8 - /bin/zsh
Binaries:
  Node: 16.15.0 - ~/.nvs/default/bin/node
  npm: 8.5.5 - ~/.nvs/default/bin/npm
Browsers:
  Firefox: 100.0
  Safari: 15.4
npmPackages:
  @sveltejs/adapter-auto: next => 1.0.0-next.42 
  @sveltejs/kit: next => 1.0.0-next.330 
  svelte: ^3.44.0 => 3.48.0 

Severity

annoyance

Additional Information

No response

@Rich-Harris Rich-Harris added the bug Something isn't working label May 14, 2022
@Rich-Harris
Copy link
Member

cc'ing @ignatiusmb for visibility

@Rich-Harris Rich-Harris added this to the 1.0 milestone May 14, 2022
@ignatiusmb
Copy link
Member

Thank you for the thorough report. Seems like the initial problem of interfaces not having implicit index signatures is still apparent when nested as properties. Changing Item from an interface to a type alias removes the error, so I guess something is not working with the recursive check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working types / typescript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants