sveltejs / sveltejs/kit

different generated types for ActionData based on how we return the data (directly or indirectly)

Open
#10,689 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

types / typescript
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the bug

I have an async method that returns a Union of types A and B (they might or might not have something in common). If I do

const a = await asyncFun()
return a;

then typings on ActionData will be correct (typeof ActionData will be {typeof A & AllUndefined<B> | typeof B & AllUndefined<A>), but if I do

return await asyncFunc()

then all typings will be wrong (typeof ActionData will be {} | null)

Reproduction

1- clone weird-type-generation brach of this repository
2- run npm i
3- run npm run check
4- at this step everything should be fine, goto src/routes/user/todos and the type of ActionData is

{
    success?: undefined;
    result?: undefined;
    message: ErrorMessage;
    error: {
        title?: string[] | undefined;
        description?: string[] | undefined;
        is_done?: string[] | undefined;
    };
} | {
    success?: undefined;
    result?: undefined;
    message: ErrorMessage;
    error: {
        title: string;
        description: string;
        is_done: boolean;
    };
} | {
    message?: undefined;
    error?: undefined;
    success: true;
    result: Todo;
} | null

5- goto src/routes/user/todos/+page.server.ts and uncommend the lines 47 to 54 where we immediatly return the awaited async funtion (I've also described the problem in the comments of mentioned lines) and comment lines 36 to 44 where we first store it in a variable and then return the variable.
6- run npm run check again.
7- now goto the corresponding +page.svelte (src/routes/user/todos/+page.svelte) and see the type of ActionData. it is {} | null now!
*** this problem exists in src/routes/signup as well (but not in src/routes/login).

Logs

No response

System Info
System:
    OS: Linux 5.15 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
    CPU: (16) x64 AMD Ryzen 7 4800H with Radeon Graphics
    Memory: 10.25 GB / 15.33 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 18.16.1 - ~/.nvm/versions/node/v18.16.1/bin/node
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.1/bin/npm
  Browsers:
    Chrome: 115.0.5790.170
  npmPackages:
    @sveltejs/adapter-auto: ^2.0.0 => 2.1.0 
    @sveltejs/kit: ^1.20.4 => 1.22.4 
    svelte: ^4.0.5 => 4.1.2 
    vite: ^4.4.2 => 4.4.9
Severity

serious, but I can work around it

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue from the linked weird-type-generation branch with npm i and npm run check. Compare src/routes/user/todos/+page.server.ts with src/routes/user/todos/+page.svelte, including the analogous signup route, then trace how ActionData types are generated. Done means direct and indirect awaited returns produce the same correct union type.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
developer-experience
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.