@sveltejs/kit
Version:
SvelteKit is the fastest way to build Svelte apps
200 lines (182 loc) • 8.41 kB
TypeScript
/**
* It's possible to tell SvelteKit how to type objects inside your app by declaring the `App` namespace. By default, a new project will have a file called `src/app.d.ts` containing the following:
*
* ```ts
* declare global {
* namespace App {
* // interface Error {}
* // interface Locals {}
* // interface PageData {}
* // interface PageState {}
* // interface Platform {}
* }
* }
*
* export {};
* ```
*
* The `export {}` line exists because without it, the file would be treated as an _ambient module_ which prevents you from adding `import` declarations.
* If you need to add ambient `declare module` declarations, do so in a separate file like `src/ambient.d.ts`.
*
* By populating these interfaces, you will gain type safety when using `event.locals`, `event.platform`, and `data` from `load` functions.
*/
declare namespace App {
/**
* Defines the common shape of expected and unexpected errors. Expected errors are thrown using the `error` function. Every error passes through the `handleError` hooks, which must return this shape (with `status` and `message` optional, since they default to those of the caught error).
*/
export interface Error {
status: number;
message: string;
}
/**
* The interface that defines `event.locals`, which can be accessed in server [hooks](https://svelte.dev/docs/kit/hooks) (`handle`, and `handleError`), server-only `load` functions, and `+server.js` files.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface Locals {}
/**
* Defines the common shape of the [page.data state](https://svelte.dev/docs/kit/$app-state#page) - that is, the data that is shared between all pages.
* The `Load` and `ServerLoad` functions in `./$types` will be narrowed accordingly.
* Use optional properties for data that is only present on specific pages. Do not add an index signature (`[key: string]: any`).
*/
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface PageData {}
/**
* The shape of the `page.state` object, which can be manipulated using [`goto`](https://svelte.dev/docs/kit/$app-navigation#goto).
*/
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface PageState {}
/**
* If your adapter provides [platform-specific context](https://svelte.dev/docs/kit/adapters#Platform-specific-context) via `event.platform`, you can specify it here.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface Platform {}
}
/**
* This module is available to [service workers](https://svelte.dev/docs/kit/service-workers) and other contexts.
* It exports information about the build output, static files, prerendered pages, and routes.
*/
declare module '$app/manifest' {
/**
* An array of `{ path: string }` objects representing the files generated by Vite.
* The path is relative to the [base path](https://svelte.dev/docs/kit/configuration#paths), and is intended for use with `cache.add(...)` inside a [service worker](https://svelte.dev/docs/kit/service-workers).
* During development, this is an empty array.
*/
export const immutable: Array<{ path: string }>;
/**
* An array of `{ path: AssetPath }` objects representing the files in your `static` directory, or whatever directory is specified by `config.files.assets`.
* The path is relative to the [base path](https://svelte.dev/docs/kit/configuration#paths), and can be used with [`asset(...)`](https://svelte.dev/docs/kit/$app-paths#asset).
*/
export const assets: Array<{ path: import('$app/types').AssetPath }>;
/**
* An array of `{ path: Path }` objects representing prerendered pages and endpoints, relative to the [base path](https://svelte.dev/docs/kit/configuration#paths).
* During development, this is an empty array.
*/
export const prerendered: Array<{ path: import('$app/types').Path }>;
/**
* A route in your app, along with its capabilities. `page` indicates the presence of a `+page`,
* while `endpoint` indicates the presence of a `+server`. Both are `true` when both files exist.
*/
export type ManifestRoute =
| {
id: Exclude<import('$app/types').PageRouteId, import('$app/types').EndpointRouteId>;
page: true;
endpoint: false;
}
| {
id: Exclude<import('$app/types').EndpointRouteId, import('$app/types').PageRouteId>;
page: false;
endpoint: true;
}
| {
id: Extract<import('$app/types').PageRouteId, import('$app/types').EndpointRouteId>;
page: true;
endpoint: true;
};
/**
* An array of objects representing the routes in your app. Only routes that the router can match
* are included — directories that merely hold a `+layout` are not routes of their own.
*
* Each object has an `id`, plus `page` and `endpoint` booleans describing whether the route has a
* `+page` and/or a `+server`. Both are `true` for a route that has both, so the capabilities can
* be filtered independently:
*
* ```js
* import { routes } from '$app/manifest';
*
* const pages = routes.filter((route) => route.page);
* const endpoints = routes.filter((route) => route.endpoint);
* ```
*/
export const routes: ManifestRoute[];
}
/**
* This module contains generated types for the routes in your app.
*/
declare module '$app/types' {
/**
* Interface for all generated app types. This gets extended via declaration merging. DO NOT USE THIS INTERFACE DIRECTLY.
*/
export interface AppTypes {
// These are all functions so that we can leverage function overloads to get the correct type.
// Using the return types directly would error with a "not the same type" error.
// https://www.typescriptlang.org/docs/handbook/declaration-merging.html#merging-interfaces
PageRouteId(): string;
EndpointRouteId(): string;
RouteId(): string;
RouteParams(): Record<string, Record<string, string>>;
LayoutParams(): Record<string, Record<string, string>>;
Path(): string;
ResolvedPathname(): string;
AssetPath(): string;
}
/**
* A union of the route IDs in your app that have a `+page`.
*
* A route ID can be in both `PageRouteId` and `EndpointRouteId`, if its directory contains both a `+page` and a `+server`.
*/
export type PageRouteId = ReturnType<AppTypes['PageRouteId']>;
/**
* A union of the route IDs in your app that have a `+server`.
*
* A route ID can be in both `PageRouteId` and `EndpointRouteId`, if its directory contains both a `+page` and a `+server`.
*/
export type EndpointRouteId = ReturnType<AppTypes['EndpointRouteId']>;
/**
* A union of all the route IDs in your app — the union of `PageRouteId` and `EndpointRouteId`. Used for `page.route.id` and `event.route.id`.
*/
export type RouteId = ReturnType<AppTypes['RouteId']>;
/**
* `RouteId`, but possibly suffixed with a search string and/or hash.
*/
export type RouteIdWithSearchOrHash = RouteId | `${RouteId}?${string}` | `${RouteId}#${string}`;
/**
* A utility for getting the parameters associated with a given route.
*/
export type RouteParams<T extends RouteId> = T extends keyof ReturnType<AppTypes['RouteParams']>
? ReturnType<AppTypes['RouteParams']>[T]
: Record<string, never>;
/**
* The route IDs accepted by `LayoutParams`. Like `RouteId`, these preserve route groups and `[param]` syntax, but they identify directories containing layouts rather than matchable routes.
*/
type LayoutParamsId = keyof ReturnType<AppTypes['LayoutParams']>;
/**
* A utility for getting the parameters associated with a given layout, which is similar to `RouteParams` but also includes optional parameters for any child route.
*/
export type LayoutParams<T extends LayoutParamsId> = ReturnType<AppTypes['LayoutParams']>[T];
/**
* A union of all valid paths in your app, relative to the `base` path.
*/
export type Path = ReturnType<AppTypes['Path']>;
/**
* `Path`, but possibly suffixed with a search string and/or hash.
*/
export type PathnameWithSearchOrHash = Path | `${Path}?${string}` | `${Path}#${string}`;
/**
* `Path`, but prefixed with a base path. Used for `page.url.pathname`.
*/
export type ResolvedPathname = ReturnType<AppTypes['ResolvedPathname']>;
/**
* A union of all the filenames of assets contained in your `static` directory, relative to the `base` path.
*/
export type AssetPath = ReturnType<AppTypes['AssetPath']>;
}