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

[docs] enhance docs on adding types to app.d.ts #5280

Merged
merged 2 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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/light-rings-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

Enhance docs on importing types in app.d.ts
1 change: 1 addition & 0 deletions packages/create-svelte/templates/default/src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
// and what to do when importing types
declare namespace App {
interface Locals {
userid: string;
Expand Down
1 change: 1 addition & 0 deletions packages/create-svelte/templates/skeleton/src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
// and what to do when importing types
declare namespace App {
// interface Locals {}
// interface Platform {}
Expand Down
18 changes: 17 additions & 1 deletion packages/kit/types/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,29 @@
*
* By populating these interfaces, you will gain type safety when using `event.locals`, `event.platform`, `session` and `stuff`.
*
* Note that since it's an ambient declaration file, you can't use `import` statements — instead, use the `import(...)` function:
* 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 {
* namespace App {
* interface Locals {
* user: User;
* }
* // ...
* }
* }
* ```
*
Comment on lines +29 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there drawbacks to wrapping the namespace with declare global? If not, wouldn't it make sense to just do this by default in create-svelte? And if there are, then it probably shouldn't be recommended without a caveat.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem is that it's a type error if there are no imports, so unfortunately we can't add it by default

*/
declare namespace App {
/**
Expand Down