sveltejs / sapper

The next small thing in web development, powered by Svelte
https://sapper.svelte.dev
MIT License
7.01k stars 435 forks source link

Cannot use namespace 'SapperRequest' as a type #1748

Open danawoodman opened 3 years ago

danawoodman commented 3 years ago

Describe the bug Trying to use type definitions of SapperRequest and SapperResponse but getting Cannot use namespace 'SapperRequest' as a type instead of types.

image

import type { SapperRequest, SapperResponse } from "@sapper/server";

export async function get(req: SapperRequest, res: SapperResponse) {
  res.json({ hello: "world" });
}

tsconfig.json:

{
  "extends": "@tsconfig/svelte/tsconfig.json",
  "compilerOptions": {
    "lib": ["DOM", "ES2017", "WebWorker"]
  },
  "include": ["src/**/*", "src/node_modules/**/*"],
  "exclude": ["node_modules/*", "__sapper__/*", "static/*"]
}

src/node_modules/@sapper/index.d.ts:

declare module '@sapper/app'
declare module '@sapper/server'
declare module '@sapper/service-worker'
declare module '@sapper/common'

declare module '@sapper/app' {
    export interface Redirect {
        statusCode: number
        location: string
    }

    export function goto(href: string, opts: { noscroll?: boolean, replaceState?: boolean }): Promise<void>;
    export function prefetch(href: string): Promise<{ redirect?: Redirect; data?: unknown }>;
    export function prefetchRoutes(pathnames: string[]): Promise<void>;
    export function start(opts: { target: Node }): Promise<void>;
    export const stores: () => unknown;
}

declare module '@sapper/server' {
    import { Handler, Req, Res } from '@sapper/internal/manifest-server';

    export type Ignore = string | RegExp | ((uri: string) => boolean) | Ignore[];

    export interface MiddlewareOptions {
        session?: (req: Req, res: Res) => unknown
        ignore?: Ignore
    }

    export function middleware(opts: MiddlewareOptions): Handler;
}

declare module '@sapper/service-worker' {
    export const timestamp: number;
    export const files: string[];
    export const assets: string[];
    export const shell: string[];
    export const routes: Array<{ pattern: RegExp }>;
}

declare module '@sapper/common' {
    export interface PreloadContext {
        fetch: (url: string, options?: any) => Promise<any>;
        error: (statusCode: number, message: Error | string) => void;
        redirect: (statusCode: number, location: string) => void;
    }

    export interface Page {
        host: string;
        path: string;
        params: Record<string, string>;
        query: Record<string, string | string[]>;
        error?: Error;
    }

    export interface Preload {
        (this: PreloadContext, page: Page, session: any): object | Promise<object>;
    }
}

Logs n/a

To Reproduce I think just start a blank Sapper repo, run Typescript setup and then create a route like above

Expected behavior Should show proper types

Stacktraces n/a

Information about your Sapper Installation:

  System:
    OS: macOS 11.2
    CPU: (8) arm64 Apple M1
    Memory: 86.23 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.8.0 - ~/.nvm/versions/node/v15.8.0/bin/node
    npm: 7.5.1 - ~/.nvm/versions/node/v15.8.0/bin/npm
  Browsers:
    Brave Browser: 89.1.21.73
    Chrome: 89.0.4389.82
    Safari: 14.0.3
  npmPackages:
    rollup: ^2.3.4 => 2.40.0
    sapper: ^0.28.0 => 0.28.10
    svelte: ^3.17.3 => 3.35.0

Severity Not critical, just annoying; can't use Sapper types

Additional context Add any other context about the problem here.

May relate to https://github.com/sveltejs/sapper/issues/1583#issuecomment-789645761