Skip to content

LayoutServerLoad return type is not compatible with simple interface object #5963

@zlepper

Description

@zlepper

Describe the bug

If you explicitly declare the return type of the load function in +layout.server.ts, then you will be told by typescript that it doesn't match the expected return type. For example:

import type { LayoutServerLoad } from './$types';

export interface Session {
	authToken: string;
}

export const load: LayoutServerLoad = ({ locals }): { session: Session } => {
	return {
		session: {
			authToken: locals.authToken,
		},
	};
};

Specifically you get this nice error:

Error: Type '({ locals }: ServerLoadEvent<LayoutParams, null>) => { session: Session; }' is not assignable to type 'LayoutServerLoad'.
  Type '{ session: Session; }' is not assignable to type 'MaybePromise<void | JSONObject>'.
    Type '{ session: Session; }' is not assignable to type 'JSONObject'.
      Property 'session' is incompatible with index signature.
        Type 'Session' is not assignable to type 'JSONValue'.
          Type 'Session' is not assignable to type 'JSONObject'.
            Index signature for type 'string' is missing in type 'Session'.

Running npm run check will output the above error if you don't get it directly in your development environment

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-o1ssqr?file=src%2Froutes%2F%2Blayout.server.ts,package.json&terminal=dev

Stackblitz can't find the ./$types so it doesn't actually reproduce the error...

Logs

No response

System Info

System:
    OS: Windows 10 10.0.19044
    CPU: (64) x64 AMD Ryzen Threadripper PRO 3975WX 32-Cores
    Memory: 31.36 GB / 127.85 GB
  Binaries:
    Node: 16.13.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.15 - C:\Program Files\nodejs\yarn.CMD
    npm: 8.1.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 104.0.5112.81
    Edge: Spartan (44.19041.1266.0), Chromium (104.0.1293.54)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.57
    @sveltejs/adapter-cloudflare: next => 1.0.0-next.31
    @sveltejs/kit: 1.0.0-next.415 => 1.0.0-next.415
    svelte: ^3.44.0 => 3.49.0
    vite: ^3.0.0 => 3.0.8

Severity

annoyance

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions