pingdotgg / uploadthing

File uploads for modern web devs
https://uploadthing.com
MIT License
3.74k stars 267 forks source link

[bug]: Typescript declaration: true not supported #790

Closed 4leite closed 1 month ago

4leite commented 2 months ago

Provide environment information

System:
    OS: Linux 6.1 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
    CPU: (6) x64 Intel(R) Core(TM) i5-9600KF CPU @ 3.70GHz
    Memory: 22.75 GB / 31.28 GB
    Container: Yes
    Shell: 5.2.15 - /bin/bash
  Binaries:
    Node: 20.12.2 - ~/.local/share/pnpm/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v21.7.1/bin/yarn
    npm: 10.5.0 - ~/.local/share/pnpm/npm
    pnpm: 8.15.7 - ~/.local/share/pnpm/pnpm
  Browsers:
    Chrome: 124.0.6367.60
    Chromium: 124.0.6367.60
  npmPackages:
    typescript: 5.4.2 => 5.4.2

Describe the bug

Getting:

src/uploadthing/file-router.ts:15:7 - error TS2742: The inferred type of 'initFileRouter' cannot be named without a reference to '.pnpm/@uploadthing+shared@6.7.1_@uploadthing+mime-types@0.2.9/node_modules/@uploadthing/shared'. This is likely not portable. A type annotation is necessary.

I'm building a library that is then consumed downstream, so declaration files are a requirement.

Link to reproduction

https://github.com/4leite/ut-declaration

To reproduce

git clone git@github.com:4leite/ut-declaration.git
cd ut-declaration
pnpm install
pnpm build

Additional information

192 was closed (maybe fixed?)

252 was closed (superseded by #272)

272 was closed (won't fix)

I've indicated I'm down to submit a PR, but looking at the talented folks who have already thrown their hands up, I'm not sure I've got any better suggestions.

๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Contributing

Code of Conduct

4leite commented 2 months ago

Workaround is to add empty imports per the error messages, eg:

import type {} from "@uploadthing/shared"
import type {} from "next/server"

edit: added type annotation per suggestion below

juliusmarminge commented 2 months ago

yea i think this is the only way.

this is an issue in the "lazyness" of TypeScript where you have nested packages it doesn't dig deep enough to find those types automatically for you.

what i have found is that using something other than pnpm solves this sometimes, as the nested node_modules/.pnpm seems to make it even harder for typescript to automatically find the types...

you can change it to import type * as UTShared from "@uploadthing/shared" so that the imports aren't present in the output javascript though.

from my understanding this is also getting improved in typescript 5.5: https://devblogs.microsoft.com/typescript/announcing-typescript-5-5-beta/#consulting-packagejson-dependencies-for-declaration-file-generation

4leite commented 2 months ago

Nice, I mostly raised the bug to document the workaround and see if there were any better solutions.

juliusmarminge commented 2 months ago

Yea - it's unfortunate we have this limitation in TypeScript right now. It's like this for a lot of libraries that has framework agnostic core packages that are not explicitely installed by the end user.

I reproduced it in ReactQuery, NextAuth and we tried extracting a bunch of core logic in trpc and ran into it there too.

Hopefully it will be a thing of the past when 5.5 comes, but in the meantime using those "fake imports" is the way i think...

github-actions[bot] commented 2 months ago

This issue has been automatically marked as stale because it has not had any activity for 10 days. It will be closed in 5 days if no further activity occurs.

github-actions[bot] commented 1 month ago

This issue has been closed because it has not had any activity for 5 days since being marked as stale.