kwhitley / itty-router

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

TS compilation error when using itty-router 5.0.17 #243

Closed b3b00 closed 6 months ago

b3b00 commented 6 months ago

Hello,

I think I am facing the same issue as #237 when using itty-router v 5.0.17

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/html.d.ts:1:35 - error TS2307: Cannot find module 'types' or its corresponding type declarations.

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

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

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

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

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

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/types/RouterOptions.d.ts:1:29 - error TS2307: Cannot find module 'StatusError' or its corresponding type declarations.

1 import { StatusError } from 'StatusError';
                              ~~~~~~~~~~~~~

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

1 import { StatusError } from 'StatusError';
                              ~~~~~~~~~~~~~

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 8 errors in 8 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/types/RouterOptions.d.ts:1
     1  node_modules/itty-router/types/RouterType.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

for html.d.ts, jpeg.d.ts, json.d.ts, png.d.ts, text.d.ts and webp.d.ts before

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

after

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

for RouterOptions.d.ts and RouterTypes.d.ts before

import { StatusError } from 'StatusError';

after

// note the "../" before StatusError
import { StatusError } from '../StatusError';

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

b3b00 commented 6 months ago

Hello @kwhitley , is there any news about this issue ?

kwhitley commented 6 months ago

Sorry this slipped by the radar, Olivier, I'll dive into it today!

kwhitley commented 6 months ago

So... I'm not seeing the erros you're seeing... when running dev which I assume is what I was supposed to run, I get the following (without errors). Only thing I saw was wrangler asking for a compatability date.

image image image
kwhitley commented 6 months ago

Which I assume is using _worker.js, not _worker.ts, if that matters (I assume it does)

b3b00 commented 6 months ago

Sorry, the issue occurs when compiling typescript To js, situer direct using tsc or with npm run build on m'y repo.

b3b00 commented 6 months ago

@kwhitley , maybe it comes from my steup. so here it is :

updatinf tsc to

kwhitley commented 6 months ago

Whoa... that's a super old version of Node (given we're on 20 and most libs barely support back as far as 16)...

Installing now to test though! :)

b3b00 commented 6 months ago

Houps this is 18.16 ☺️

b3b00 commented 6 months ago

The same errors happen on cloudflare when deploying. There versions are : Node 18.17.109 Nom 9.6.7 Typescript 5.4.5

b3b00 commented 6 months ago

So i 've finally found a way to fix this compilation issue. It was an tsconfig.json problem. See my commit here : https://github.com/b3b00/minimal-cloudflare-itty-router-5/commit/0641513d834a65087cd0c4f8a5490feb6f91219b The interresting part is

 "skipLibCheck": true,

Thanks so much for your time trying to help. Hope this could help other users facing the same issue.

kwhitley commented 6 months ago

Nice catch! I'll have to do some testing with tsconfig flags and find the interactions that users may get stuck in...

Def need a trouble shooting guide... if nothing else for the hung promise folks get when still using router.handle, haha.

zdmc23 commented 5 months ago

This continues to be an issue even after updating tsconfig.json

[vite:dts] Internal Error: Unable to follow symbol for "StatusError"

You have encountered a software defect. Please consider reporting the issue to the maintainers of this application.
    at AstSymbolTable._analyzeChildTree (/home/zmccoy/sandbox/unicornx-api/node_modules/.pnpm/@microsoft+api-extractor@7.43.0_@types+node@20.14.2/node_modules/@microsoft/api-extractor/lib/analyzer/AstSymbolTable.js:328:43)
    at AstSymbolTable._analyzeChildTree (/home/zmccoy/sandbox/unicornx-api/node_modules/.pnpm/@microsoft+api-extractor@7.43.0_@types+node@20.14.2/node_modules/@microsoft/api-extractor/lib/analyzer/AstSymbolTable.js:377:18)
    at AstSymbolTable._analyzeChildTree (/home/zmccoy/sandbox/unicornx-api/node_modules/.pnpm/@microsoft+api-extractor@7.43.0_@types+node@20.14.2/node_modules/@microsoft/api-extractor/lib/analyzer/AstSymbolTable.js:377:18)
    at AstSymbolTable._analyzeChildTree (/home/zmccoy/sandbox/unicornx-api/node_modules/.pnpm/@microsoft+api-extractor@7.43.0_@types+node@20.14.2/node_modules/@microsoft/api-extractor/lib/analyzer/AstSymbolTable.js:377:18)
    at AstSymbolTable._analyzeChildTree (/home/zmccoy/sandbox/unicornx-api/node_modules/.pnpm/@microsoft+api-extractor@7.43.0_@types+node@20.14.2/node_modules/@microsoft/api-extractor/lib/analyzer/AstSymbolTable.js:377:18)
    at AstSymbolTable._analyzeChildTree (/home/zmccoy/sandbox/unicornx-api/node_modules/.pnpm/@microsoft+api-extractor@7.43.0_@types+node@20.14.2/node_modules/@microsoft/api-extractor/lib/analyzer/AstSymbolTable.js:377:18)
    at AstSymbolTable._analyzeChildTree (/home/zmccoy/sandbox/unicornx-api/node_modules/.pnpm/@microsoft+api-extractor@7.43.0_@types+node@20.14.2/node_modules/@microsoft/api-extractor/lib/analyzer/AstSymbolTable.js:377:18)
    at AstSymbolTable._analyzeChildTree (/home/zmccoy/sandbox/unicornx-api/node_modules/.pnpm/@microsoft+api-extractor@7.43.0_@types+node@20.14.2/node_modules/@microsoft/api-extractor/lib/analyzer/AstSymbolTable.js:377:18)
    at AstSymbolTable._analyzeChildTree (/home/zmccoy/sandbox/unicornx-api/node_modules/.pnpm/@microsoft+api-extractor@7.43.0_@types+node@20.14.2/node_modules/@microsoft/api-extractor/lib/analyzer/AstSymbolTable.js:377:18)
    at AstSymbolTable._analyzeAstSymbol (/home/zmccoy/sandbox/unicornx-api/node_modules/.pnpm/@microsoft+api-extractor@7.43.0_@types+node@20.14.2/node_modules/@microsoft/api-extractor/lib/analyzer/AstSymbolTable.js:247:18)
 ELIFECYCLE  Command failed with exit code 1.

@b3b00 's fix remains the same:

for RouterOptions.d.ts and RouterTypes.d.ts

before

import { StatusError } from 'StatusError';

after

// note the "../" before StatusError
import { StatusError } from '../StatusError';

So, if you want to build a library and include this package (via some external CI process) then you will need to patch the package post-install, or revert to most recent version without the issue: 5.0.12