Skip to content

Commit 5db49d5

Browse files
dummdidummSimongtm-nayan
authored
[docs] enhance docs on adding types to app.d.ts (#5280)
* [docs] enhance docs on adding types to app.d.ts * Update packages/kit/types/ambient.d.ts Co-authored-by: gtmnayan <[email protected]> Co-authored-by: Simon <[email protected]> Co-authored-by: gtmnayan <[email protected]>
1 parent 4f55588 commit 5db49d5

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

.changeset/light-rings-trade.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'create-svelte': patch
3+
---
4+
5+
Enhance docs on importing types in app.d.ts

packages/create-svelte/templates/default/src/app.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// See https://kit.svelte.dev/docs/types#app
44
// for information about these interfaces
5+
// and what to do when importing types
56
declare namespace App {
67
interface Locals {
78
userid: string;

packages/create-svelte/templates/skeleton/src/app.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// See https://kit.svelte.dev/docs/types#app
44
// for information about these interfaces
5+
// and what to do when importing types
56
declare namespace App {
67
// interface Locals {}
78
// interface Platform {}

packages/kit/types/ambient.d.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,29 @@
1717
*
1818
* By populating these interfaces, you will gain type safety when using `event.locals`, `event.platform`, `session` and `stuff`.
1919
*
20-
* Note that since it's an ambient declaration file, you can't use `import` statements — instead, use the `import(...)` function:
20+
* Note that since it's an ambient declaration file, you have to be careful when using `import` statements. Once you add an `import`
21+
* at the top level, the declaration file is no longer considered ambient and you lose access to these typings in other files.
22+
* To avoid this, either use the `import(...)` function:
2123
*
2224
* ```ts
2325
* interface Locals {
2426
* user: import('$lib/types').User;
2527
* }
2628
* ```
29+
* Or wrap the namespace with `declare global`:
30+
* ```ts
31+
* import { User } from '$lib/types';
32+
*
33+
* declare global {
34+
* namespace App {
35+
* interface Locals {
36+
* user: User;
37+
* }
38+
* // ...
39+
* }
40+
* }
41+
* ```
42+
*
2743
*/
2844
declare namespace App {
2945
/**

0 commit comments

Comments
 (0)