-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add ambient $app
types
#917
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'create-svelte': patch | ||
'@sveltejs/kit': patch | ||
--- | ||
|
||
Add ambient type definitions for \$app imports |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
/** | ||
* @type {import('$app/env').browser} | ||
*/ | ||
export const browser = !import.meta.env.SSR; | ||
/** | ||
* @type {import('$app/env').dev} | ||
*/ | ||
export const dev = !!import.meta.env.DEV; | ||
/** | ||
* @type {import('$app/env').amp} | ||
*/ | ||
export const amp = !!import.meta.env.VITE_SVELTEKIT_AMP; | ||
|
||
export { prerendering } from '../env.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
declare module '$app/env' { | ||
/** | ||
* Whether or not app is in AMP mode. | ||
*/ | ||
export const amp: boolean; | ||
/** | ||
* Whether the app is running in the browser or on the server. | ||
*/ | ||
export const browser: boolean; | ||
/** | ||
* `true` in development mode, `false` in production. | ||
*/ | ||
export const dev: boolean; | ||
} | ||
|
||
declare module '$app/navigation' { | ||
/** | ||
* Returns a Promise that resolves when SvelteKit navigates (or fails to navigate, in which case the promise rejects) to the specified href. | ||
* | ||
* @param href Where to navigate to | ||
* @param opts Optional. If `replaceState` is `true`, a new history entry won't be created. If `noscroll` is `true`, the browser won't scroll to the top of the page after navigation. | ||
*/ | ||
export function goto( | ||
href: string, | ||
opts?: { replaceState?: boolean; noscroll?: boolean } | ||
): Promise<any>; | ||
/** | ||
* Programmatically prefetches the given page, which means a) ensuring that the code for the page is loaded, and b) calling the page's load function with the appropriate options. | ||
* This is the same behaviour that SvelteKit triggers when the user taps or mouses over an `<a>` element with `sveltekit:prefetch`. | ||
* If the next navigation is to `href`, the values returned from load will be used, making navigation instantaneous. | ||
* Returns a Promise that resolves when the prefetch is complete. | ||
* | ||
* @param href Page to prefetch | ||
*/ | ||
export function prefetch(href: string): Promise<any>; | ||
/** | ||
* Programmatically prefetches the code for routes that haven't yet been fetched. | ||
* Typically, you might call this to speed up subsequent navigation. | ||
* If no argument is given, all routes will be fetched, otherwise you can specify routes by any matching pathname such as `/about` (to match `src/routes/about.svelte`) | ||
* or `/blog/*` (to match `src/routes/blog/[slug].svelte`). Unlike prefetch, this won't call preload for individual pages. | ||
* Returns a Promise that resolves when the routes have been prefetched. | ||
*/ | ||
export function prefetchRoutes(routes?: string[]): Promise<any>; | ||
} | ||
|
||
declare module '$app/paths' { | ||
/** | ||
* A root-relative (i.e. begins with a `/`) string that matches `config.kit.files.base` in your project configuration. | ||
*/ | ||
export const base: string; | ||
/** | ||
* A root-relative or absolute path that matches `config.kit.files.assets` (after it has been resolved against base). | ||
*/ | ||
export const assets: string; | ||
} | ||
|
||
declare module '$app/stores' { | ||
import { Readable, Writable } from 'svelte/store'; | ||
import { Page } from '@sveltejs/kit'; | ||
|
||
/** | ||
* A convenience function around `getContext` that returns `{ navigating, page, session }`. | ||
* Most of the time, you won't need to use it. | ||
*/ | ||
export function getStores(): { | ||
navigating: Readable<{ from: string; to: string } | null>; | ||
page: Readable<Page>; | ||
session: Writable<any>; | ||
}; | ||
/** | ||
* A readable store whose value reflects the object passed to load functions. | ||
*/ | ||
export const page: Readable<Page>; | ||
/** | ||
* A readable store. | ||
* When navigating starts, its value is `{ from, to }`, where from and to both mirror the page store value. | ||
* When navigating finishes, its value reverts to `null`. | ||
*/ | ||
export const navigating: Readable<{ from: string; to: string } | null>; | ||
/** | ||
* A writable store whose initial value is whatever was returned from `getSession`. | ||
* It can be written to, but this will not cause changes to persist on the server — this is something you must implement yourself. | ||
*/ | ||
export const session: Writable<any>; | ||
} | ||
|
||
declare module '$service-worker' { | ||
/** | ||
* An array of URL strings representing the files generated by Vite, suitable for caching with `cache.addAll(build)`. | ||
* This is only available to service workers. | ||
*/ | ||
export const build: string[]; | ||
/** | ||
* An array of URL strings representing the files in your static directory, | ||
* or whatever directory is specified by `config.kit.files.assets`. | ||
* This is only available to service workers. | ||
*/ | ||
export const assets: string[]; | ||
/** | ||
* The result of calling `Date.now()` at build time. | ||
* It's useful for generating unique cache names inside your service worker, | ||
* so that a later deployment of your app can invalidate old caches. | ||
* This is only available to service workers. | ||
*/ | ||
export const timestamp: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand what "ambient" means here. It means the types are just defined in a
.d.ts
file? But aren't the types in this file also ambient types?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I adopted the word "ambient" since the official TS docs also use it when declaring modules: https://www.typescriptlang.org/docs/handbook/modules.html#ambient-modules
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite following why the types are split into two files and how it's decided what is in each file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything in ambient needs to be in its own file because this file cannot have top level imports or it no longer works (this is how ts works, don't ask me why). The split between internal and public - I don't know.