Skip to content

Commit 158ec86

Browse files
fix: adjust app.d.ts to diminish confusion about imports (#8477)
* fix: adjust `app.d.ts` to diminish confusion about imports closes #8276 * Apply suggestions from code review * Update packages/kit/types/ambient.d.ts Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
1 parent 6c7f9b2 commit 158ec86

File tree

5 files changed

+39
-46
lines changed

5 files changed

+39
-46
lines changed

.changeset/selfish-tools-prove.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'create-svelte': minor
3+
---
4+
5+
fix: adjust `app.d.ts` to diminish confusion about imports
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
// See https://kit.svelte.dev/docs/types#app
22
// for information about these interfaces
33
// and what to do when importing types
4-
declare namespace App {
5-
// interface Error {}
6-
// interface Locals {}
7-
// interface PageData {}
8-
// interface Platform {}
4+
declare global {
5+
namespace App {
6+
// interface Error {}
7+
// interface Locals {}
8+
// interface PageData {}
9+
// interface Platform {}
10+
}
911
}
12+
13+
export {};
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
// See https://kit.svelte.dev/docs/types#app
22
// for information about these interfaces
33
// and what to do when importing types
4-
declare namespace App {
5-
// interface Error {}
6-
// interface Locals {}
7-
// interface PageData {}
8-
// interface Platform {}
4+
declare global {
5+
namespace App {
6+
// interface Error {}
7+
// interface Locals {}
8+
// interface PageData {}
9+
// interface Platform {}
10+
}
911
}
12+
13+
export {};
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
/// <reference types="@sveltejs/kit" />
2-
31
// See https://kit.svelte.dev/docs/types#app
42
// for information about these interfaces
53
// and what to do when importing types
6-
declare namespace App {
7-
// interface Error {}
8-
// interface Locals {}
9-
// interface PageData {}
10-
// interface Platform {}
4+
declare global {
5+
namespace App {
6+
// interface Error {}
7+
// interface Locals {}
8+
// interface PageData {}
9+
// interface Platform {}
10+
}
1111
}
12+
13+
export {};

packages/kit/types/ambient.d.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,19 @@
22
* 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:
33
*
44
* ```ts
5-
* /// <reference types="@sveltejs/kit" />
6-
*
7-
* declare namespace App {
8-
* interface Error {}
9-
* interface Locals {}
10-
* interface PageData {}
11-
* interface Platform {}
12-
* }
13-
* ```
14-
*
15-
* By populating these interfaces, you will gain type safety when using `event.locals`, `event.platform`, and `data` from `load` functions.
16-
*
17-
* Note that since it's an ambient declaration file, you have to be careful when using `import` statements. Once you add an `import`
18-
* at the top level, the declaration file is no longer considered ambient and you lose access to these typings in other files.
19-
* To avoid this, either use the `import(...)` function:
20-
*
21-
* ```ts
22-
* interface Locals {
23-
* user: import('$lib/types').User;
24-
* }
25-
* ```
26-
* Or wrap the namespace with `declare global`:
27-
* ```ts
28-
* import { User } from '$lib/types';
29-
*
305
* declare global {
316
* namespace App {
32-
* interface Locals {
33-
* user: User;
34-
* }
35-
* // ...
7+
* // interface Error {}
8+
* // interface Locals {}
9+
* // interface PageData {}
10+
* // interface Platform {}
3611
* }
3712
* }
13+
*
14+
* export default undefined;
3815
* ```
3916
*
17+
* By populating these interfaces, you will gain type safety when using `event.locals`, `event.platform`, and `data` from `load` functions.
4018
*/
4119
declare namespace App {
4220
/**

0 commit comments

Comments
 (0)