Skip to content
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

fix: adjust app.d.ts to diminish confusion about imports #8477

Merged
merged 3 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/selfish-tools-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': minor
---

fix: adjust `app.d.ts` to diminish confusion about imports
16 changes: 11 additions & 5 deletions packages/create-svelte/templates/default/src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
// and what to do when importing types
declare namespace App {
// interface Error {}
// interface Locals {}
// interface PageData {}
// interface Platform {}
declare global {
namespace App {
// interface Error {}
// interface Locals {}
// interface PageData {}
// interface Platform {}
}
}

// This line ensures this file is treated as a module.
// It can be removed if you add an import to this file.
export default undefined;
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 11 additions & 5 deletions packages/create-svelte/templates/skeleton/src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
// and what to do when importing types
declare namespace App {
// interface Error {}
// interface Locals {}
// interface PageData {}
// interface Platform {}
declare global {
namespace App {
// interface Error {}
// interface Locals {}
// interface PageData {}
// interface Platform {}
}
}

// This line ensures this file is treated as a module.
// It can be removed if you add an import to this file.
export default undefined;
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
18 changes: 11 additions & 7 deletions packages/create-svelte/templates/skeletonlib/src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/// <reference types="@sveltejs/kit" />

// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
// and what to do when importing types
declare namespace App {
// interface Error {}
// interface Locals {}
// interface PageData {}
// interface Platform {}
declare global {
namespace App {
// interface Error {}
// interface Locals {}
// interface PageData {}
// interface Platform {}
}
}

// This line ensures this file is treated as a module.
// It can be removed if you add an import to this file.
export default undefined;
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
43 changes: 14 additions & 29 deletions packages/kit/types/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,26 @@
* 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
* /// <reference types="@sveltejs/kit" />
*
* declare namespace App {
* interface Error {}
* interface Locals {}
* interface PageData {}
* interface Platform {}
* }
* ```
*
* By populating these interfaces, you will gain type safety when using `event.locals`, `event.platform`, and `data` from `load` functions.
*
* Note that since it's an ambient declaration file, you have to be careful when using `import` statements. Once you add an `import`
* at the top level, the declaration file is no longer considered ambient and you lose access to these typings in other files.
* To avoid this, either use the `import(...)` function:
*
* ```ts
* interface Locals {
* user: import('$lib/types').User;
* }
* ```
* Or wrap the namespace with `declare global`:
* ```ts
* import { User } from '$lib/types';
*
* declare global {
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
* namespace App {
* interface Locals {
* user: User;
* }
* // ...
* // interface Error {}
* // interface Locals {}
* // interface PageData {}
* // interface Platform {}
* }
* }
*
* export default undefined;
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
* ```
*
* By populating these interfaces, you will gain type safety when using `event.locals`, `event.platform`, and `data` from `load` functions.
*
* Note that since it's a declaration file, you have to be careful when using `import` statements. Once you add an `import`
* at the top level, the declaration file is no longer considered ambient, at which point you need to wrap `App` inside `declare global`
* to still be available throughout the app. Opposite to that is the behavior when there's no `import`, at which point having `declare global`
* _prevents_ the `App` types from being available.
* To safe you the headache of converting between the two this we provide a dummy `export` to force the file being a module.
* Do not remove it unless you have an `import` statement at the top level.
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
*/
declare namespace App {
/**
Expand Down