Globally Include Types in SvelteKit Project

Viewed 145

SvelteKit v1.0.0-next.354

In all my stores, components, and APIs, I have to manually import my types in order to use them in order to prevent TypeScript errors in VS Code:

import type { MyType } from '$lib/types`

I tried putting my class inside app.d.ts, but that didn't work.

declare namespace App{
  export class Aircraft{
    ...
  }
}

This gets pretty cumbersome to do across a project that is getting quite large with dozens of files.

Is there some way to globally include all my types in one place so that they are available in any file across my project? I'm very new to TypeScript and am not sure how this works.

1 Answers

When you use a namespace you have to use the type with said namespace as a prefix, so something like:

const a: App.Aircraft = { ... };

If you do not want that, you should declare the type directly:

declare class Aircraft {
  ...
}

Then you can just use:

const a: Aircraft = { ... };

Note that this only makes the type definitions available, not the type implementation. As you declared this as class rather than interface, this suggests that this type can be constructed with new. Only having the type in scope will not allow that.

Related