Nausika
Nausika

Reputation: 745

SvelteKit, import type LayoutServerLoad/PageLoad

In layout.server.ts I try to

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

but the type can't be found:

'"./$types"' has no exported member named 'LayoutServerLoad'. Did you mean 'LayoutServerData'?

What do I need to do to get the type LayoutServerLoad (or PageLoad or PageServerLoad ...) in ./$types?

Upvotes: 30

Views: 14663

Answers (3)

brunnerh
brunnerh

Reputation: 184516

  • The file has to be called +layout.server.ts (or for another load action, one of the other fixed names, see docs)
  • The Vite dev server has to be running, which watches the files and generates the types when a file is changed. (You can also execute svelte-kit sync to generate those files.)
  • (The tsconfig.json has to extend .svelte-kit\tsconfig.json, which defines rootDirs, so the generated types are resolved. This should already be the case, judging by the suggestion for LayoutServerData.)

Upvotes: 41

Rio Rifaldi
Rio Rifaldi

Reputation: 71

in my case typescript complaining on +page.ts when i import

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

and then i try run

pnpm update
pnpm check 

after that type Cmd + shift + P > restart TS server or developers: Reload window (visual studio code)

by the way if you facing typescript complaining and unsure about it, you can run step above. it's worked for me as long i coded with typescript

Upvotes: 1

Maxime Curon
Maxime Curon

Reputation: 41

For me, just an "npm update" worked.

Upvotes: 4

Related Questions