kwhitley / itty-router

A little router.
MIT License
1.8k stars 78 forks source link

typescript compilation error when using itty-router 5.0.5 #237

Closed b3b00 closed 7 months ago

b3b00 commented 7 months ago

Hello,

First a great thanks for itty-router. It's really simple and pleasant to use.

I am trying to migrate an app from itty-router 4.0 to itty 5.0 and facing a typescript compilation error With a minimal _worker.ts

import { Router, AutoRouter, IttyRouter, IRequest } from 'itty-router'

const router = AutoRouter()

router.get('/test',(request : IRequest) => {
    return {'result':'OK'};
});

router.all('*',  (request:IRequest, env:any) => {
    console.log(`assets handler ${request.url} ${request.path}`)
    return env.ASSETS.fetch(request.url)
});

export default { ...router }

I get the following errors

node_modules/itty-router/png.d.ts:1:34 - error TS2307: Cannot find module 'types' or its corresponding type declarations.

1 export declare const png: import("types").ResponseFormatter;
                                   ~~~~~~~

node_modules/itty-router/text.d.ts:1:35 - error TS2307: Cannot find module 'types' or its corresponding type declarations.

1 export declare const text: import("types").ResponseFormatter;
                                    ~~~~~~~

node_modules/itty-router/webp.d.ts:1:35 - error TS2307: Cannot find module 'types' or its corresponding type declarations.

1 export declare const webp: import("types").ResponseFormatter;
                                    ~~~~~~~

Found 6 errors in 6 files.

Errors  Files
     1  node_modules/itty-router/html.d.ts:1
     1  node_modules/itty-router/jpeg.d.ts:1
     1  node_modules/itty-router/json.d.ts:1
     1  node_modules/itty-router/png.d.ts:1
     1  node_modules/itty-router/text.d.ts:1
     1  node_modules/itty-router/webp.d.ts:1

I tried to fix it modifying the 6 mentionned *.d.ts files as below

before

export declare const html: import("types").ResponseFormatter;

after

// note the "./" before types
export declare const html: import("./types").ResponseFormatter;

I works as expected but I should not have to do this kind of hackish thing. Is there something wrong ?

I 've made a full minimal reproduction case here : https://github.com/b3b00/minimal-cloudflare-itty-router-5

thanks for your work,

Olivier

kwhitley commented 7 months ago

Hey @b3b00! Thanks for the catch great issue documentation here!

Mind giving 5.0.11 a try and see if the issue is resolved? I intend to bundle/consolidate the types in a future release, but in the meantime, I did notice that ./types was being specifically excluded from the bundler, which would in turn, remove it from the package.json export list (likely causing your issue).

Hoping this small change resolves things until I can dive back into the build system later!

b3b00 commented 7 months ago

Thanks @kwhitley, compilation IS ok now on cloudflare worker 👍

kwhitley commented 7 months ago

Glad to hear it, thanks for confirming!!