-
Describe the bugWhen I define an interface in app.d.ts that belongs to the App namespace, I want to use a type defined in another .ts file. I can import my type into app.d.ts, but when I use for example App.Locals in hooks, the type is not correctly identified in VSCode. ReproductionCreate a new SvelteKit project with app.d.ts like this: /// <reference types="@sveltejs/kit" />
// See https://kit.svelte.dev/docs#typescript
// for information about these interfaces
declare namespace App {
import { User } from './routes/test';
interface Locals {
user: User;
}
interface Platform {}
interface Session {}
interface Stuff {}
} and in routes a file import type { RequestHandler } from '@sveltejs/kit';
export interface User {
id: number;
}
export const get: RequestHandler = async ({ locals }) => {
let { id } = locals.user;
id = 'test';
return {
status: 200,
body: id
};
}; I expected an error, because id should be a number. But the type is not correctly identified in VSCode IDE. LogsNo response System InfoSystem:
OS: macOS 12.2
CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 7.76 GB / 32.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 14.17.3 - ~/n/bin/node
Yarn: 1.22.11 - ~/n/bin/yarn
npm: 7.20.0 - ~/n/bin/npm
Browsers:
Chrome: 98.0.4758.80
Firefox: 96.0.2
Safari: 15.3
npmPackages:
@sveltejs/adapter-auto: next => 1.0.0-next.17
@sveltejs/kit: next => 1.0.0-next.260
svelte: ^3.44.0 => 3.46.4 Severityserious, but I can work around it Additional InformationNo response |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 1 reply
-
The If you need custom types for the Instead of: export type User = {}; do: declare global {
type User = {};
} You'll be able to use the |
Beta Was this translation helpful? Give feedback.
-
When you put an import inside a declaration file it turns into an es6 module (except when the import is under a module declaration), Thus the declaration of To fix the issue simply wrap the namespace declaration with /// <reference types="@sveltejs/kit" />
// See https://kit.svelte.dev/docs#typescript
// for information about these interfaces
import type { User } from './routes/foo';
declare global {
declare namespace App {
interface Locals {
user: User;
}
interface Platform {}
interface Session {}
interface Stuff {}
}
} |
Beta Was this translation helpful? Give feedback.
-
Both solutions work and give to different views on the problem. Thanks a lot for the help, @f-elix and @JeanJPNM! |
Beta Was this translation helpful? Give feedback.
-
You can also do: /// <reference types="@sveltejs/kit" />
declare namespace App {
import type { BackgroundJobQueue } from 'backend-common';
interface Locals {
jobQueue: BackgroundJobQueue;
}
interface Platform {}
interface Session {}
interface Stuff {}
} |
Beta Was this translation helpful? Give feedback.
-
How are
When and why and how would I use
If I create a project with just javascript, there is no |
Beta Was this translation helpful? Give feedback.
When you put an import inside a declaration file it turns into an es6 module (except when the import is under a module declaration), Thus the declaration of
App
stops being global and you are left with the default one.To fix the issue simply wrap the namespace declaration with
declare global
like this: