Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding "paths" and "baseUrl" to tsconfig.json breaks module augmentations #254

Closed
zomars opened this issue Jul 26, 2022 · 12 comments · Fixed by #255
Closed

Adding "paths" and "baseUrl" to tsconfig.json breaks module augmentations #254

zomars opened this issue Jul 26, 2022 · 12 comments · Fixed by #255
Labels
🐞 confirmed bug Something isn't working

Comments

@zomars
Copy link
Contributor

zomars commented Jul 26, 2022

Describe the bug
It seems like the declared next-auth.d.ts is overriding the initial next-auth types instead of augmenting them.

To Reproduce
Steps to reproduce the behavior:

  1. pnpm dlx create-t3-app@next
  2. Select all packages
  3. See src/pages/api/auth/[...nextauth].ts file

Expected behavior

Having default next-auth types PLUS the augmented ones.

Screenshots

image

image

zomars added a commit to zomars/create-t3-app that referenced this issue Jul 26, 2022
@nexxeln
Copy link
Member

nexxeln commented Jul 26, 2022

Unable to reproduce, works fine for me.
user has User type

Have you tried restarting your TypeScript server?

zomars added a commit to zomars/create-t3-app that referenced this issue Jul 26, 2022
@nexxeln nexxeln added 🚫 cant reproduce The bug described could not be reproduced and removed 🐞❔ unconfirmed bug labels Jul 26, 2022
@zomars
Copy link
Contributor Author

zomars commented Jul 26, 2022

@nexxeln it may be out of scope, but it seems this happens as soon as you add path aliases to the tsconfig.json (took them from this project btw):

image

@nexxeln
Copy link
Member

nexxeln commented Jul 26, 2022

Yup thats the issue, I added path aliases and am now getting a type error.

@nexxeln nexxeln added 🐞 confirmed bug Something isn't working and removed 🚫 cant reproduce The bug described could not be reproduced labels Jul 26, 2022
nexxeln pushed a commit that referenced this issue Jul 26, 2022
@zomars
Copy link
Contributor Author

zomars commented Jul 26, 2022

It seems like even with #255 adding both "paths" and "baseUrl" to tsconfig.json keeps messing up with the module augmentations.

@zomars zomars changed the title next-auth.d.ts is conflicting with original typings Adding "paths" and "baseUrl" to tsconfig.json breaks module augmentations Jul 26, 2022
@nexxeln nexxeln reopened this Jul 26, 2022
@nexxeln
Copy link
Member

nexxeln commented Jul 26, 2022

It seems like even with #255 adding both "paths" and "baseUrl" to tsconfig.json keeps messing up with the module augmentations.

Yeah I'm not sure how to fix this, I'll open an issue in next-auth

@kroucher
Copy link
Collaborator

Some info for troubleshooting, when the next-auth.d.ts file is moved into any subdirectory, the types start working again.

image

Not sure if having a 'types' folder by default is too opinionated, but either way, the issue lies with the next-auth.d.ts file being in the root directory.

@JohnDaly
Copy link
Contributor

I noticed that changing the filename of next-auth.d.ts to something else (e.g. custom-next-auth.d.ts) will clear the type errors as well

@nexxeln
Copy link
Member

nexxeln commented Jul 26, 2022

Thank you for sharing @JohnDaly @kroucher

I'll mention this in the next-auth issue too, not sure if they have a better solution.

@JohnDaly
Copy link
Contributor

Looks like TypeScript resolves the next-auth import to the next-auth.d.ts file, and doesn't go looking for the types in node_modules. When the name is different, it is able to find the types.

Here's the output from running tsc --traceResolution:

With next-auth.d.ts

======== Resolving module 'next-auth' from '/my-t3-app/next-auth.d.ts'. ========
Explicitly specified module resolution kind: 'NodeJs'.
'baseUrl' option is set to '/my-t3-app', using this value to resolve non-relative module name 'next-auth'.
'paths' option is specified, looking for a pattern to match module name 'next-auth'.
'baseUrl' option is set to '/my-t3-app', using this value to resolve non-relative module name 'next-auth'.
Resolving module name 'next-auth' relative to base url '/my-t3-app' - '/my-t3-app/next-auth'.
Loading module as file / folder, candidate module location '/my-t3-app/next-auth', target file type 'TypeScript'.
File '/my-t3-app/next-auth.ts' does not exist.
File '/my-t3-app/next-auth.tsx' does not exist.
File '/my-t3-app/next-auth.d.ts' exist - use it as a name resolution result.
======== Module name 'next-auth' was successfully resolved to '/my-t3-app/next-auth.d.ts'. ========

With custom-next-auth.d.ts

======== Resolving module 'next-auth' from '/my-t3-app/custom-next-auth.d.ts'. ========
Explicitly specified module resolution kind: 'NodeJs'.
'baseUrl' option is set to '/my-t3-app', using this value to resolve non-relative module name 'next-auth'.
'paths' option is specified, looking for a pattern to match module name 'next-auth'.
'baseUrl' option is set to '/my-t3-app', using this value to resolve non-relative module name 'next-auth'.
Resolving module name 'next-auth' relative to base url '/my-t3-app' - '/my-t3-app/next-auth'.
Loading module as file / folder, candidate module location '/my-t3-app/next-auth', target file type 'TypeScript'.
File '/my-t3-app/next-auth.ts' does not exist.
File '/my-t3-app/next-auth.tsx' does not exist.
File '/my-t3-app/next-auth.d.ts' does not exist.
Directory '/my-t3-app/next-auth' does not exist, skipping all lookups in it.
Loading module 'next-auth' from 'node_modules' folder, target file type 'TypeScript'.
Found 'package.json' at '/my-t3-app/node_modules/next-auth/package.json'.
'package.json' does not have a 'typesVersions' field.
File '/my-t3-app/node_modules/next-auth.ts' does not exist.
File '/my-t3-app/node_modules/next-auth.tsx' does not exist.
File '/my-t3-app/node_modules/next-auth.d.ts' does not exist.
'package.json' does not have a 'typings' field.
'package.json' has 'types' field 'index.d.ts' that references '/my-t3-app/node_modules/next-auth/index.d.ts'.
File '/my-t3-app/node_modules/next-auth/index.d.ts' exist - use it as a name resolution result.
Resolving real path for '/my-t3-app/node_modules/next-auth/index.d.ts', result '/my-t3-app/node_modules/.pnpm/[email protected].../node_modules/next-auth/index.d.ts'.
======== Module name 'next-auth' was successfully resolved to '/my-t3-app/node_modules/.pnpm/[email protected].../node_modules/next-auth/index.d.ts' with Package ID 'next-auth/[email protected]'. ========

@juliusmarminge
Copy link
Member

juliusmarminge commented Jul 26, 2022

@nexxeln it may be out of scope, but it seems this happens as soon as you add path aliases to the tsconfig.json (took them from this project btw):

that is correct. seems like when adding aliasing it doesn't find the type definition or something clashes :/

stumbled upon this a while back (and also helped someone in discord with similar issue) and cut this issue (vercel/next.js#38571) for Next.js along with it.

The fix I had was just moving the file somewhere into src and then typescript found it. The problem then (as described in the issue) is that Next.js's typedefinition is in root and your others are in src(/types) which is not ideal imo.

They converted it to a discussion and since I haven't heard anything of it though...

@nexxeln
Copy link
Member

nexxeln commented Jul 26, 2022

@nexxeln it may be out of scope, but it seems this happens as soon as you add path aliases to the tsconfig.json (took them from this project btw):

that is correct. seems like when adding aliasing it doesn't find the type definition or something clashes :/

stumbled upon this a while back (and also helped someone in discord with similar issue) and cut this issue (vercel/next.js#38571) for Next.js along with it.

The fix I had was just moving the file somewhere into src and then typescript found it. The problem then (as described in the issue) is that Next.js's typedefinition is in root and your others are in src(/types) which is not ideal imo.

They converted it to a discussion and since I haven't heard anything of it though...

Makes sense, should we close and pin this then?

@zomars
Copy link
Contributor Author

zomars commented Jul 26, 2022

I think renaming to something like custom-next-auth.d.ts would be a sensible approach. It would make it explicit that we're augmenting a module instead of declaring an untyped one.

juliusmarminge added a commit that referenced this issue Aug 16, 2022
* chore(docs): updated readme to contain @next

* refactor: inject curried `runPkgManagerInstall` to package installers (#202)

* refactor: export curryable `runPkgManagerInstallOptions`

* refactor: inject curried `runPkgManagerInstall` to installers

* refactor: use curried function in installers

* fix: refactored calls that should have passed `devMode` flag

* fix: typo

* feat: migrate env + next config to esm (#205)

* chore: move lintstaged config into file (#217)

* fix: move gitignore rename call (#215)

* feat: add trpc inference helpers

* fix(#218): lock react-query to v3

* chore(release): 5.1.0

* fix: add missing trpc import

* chore(release): 5.1.1

* fix: handle pkg with version in noInstall mode (#220)

* fix: typo in readme

* feat: add db.sqlite-journal to gitignore (#221)

Co-authored-by: Shoubhit Dash <[email protected]>

* fix: typo - rename 'varibles' to 'variables' (#223)

* fix: missing DATABASE_URL (#222)

* chore(release): 5.2.0

* fix: update url in auth prisma schema

* chore(release): 5.2.1

* feat: add typesafe client side env variables (#209)

* feat: add typesafe client side env variables

* refactor: clean up type-safe client side env vars

* fix: add inferProcedureInput/Output imports in trpc utils

* refactor: client env validation

* refactor: run prettier

* refactor: clean up files and formatter func

* fix: validate public env vars are not in server schema

* fix: add ts-check to mjs files

Co-authored-by: Shoubhit Dash <[email protected]>

* fix: title being offset when using yarn/pnpm

* chore(release): bump beta version

* chore: bump Next.js to 12.2.3

* chore(release): bump beta version

* feat: improved logo typography (#238)

* chore: bump beta version

* fix: remove semicolon in _app.tsx with next-auth to avoid early return

* chore: bump beta version

* chore: add protected trpc router example (#194)

* chore: add link to community (#229)

* chore: add link to community info

* chore: add discord badge

* fix: t3 discord id

* fix: discord link on badge

Co-authored-by: Shoubhit Dash <[email protected]>

* docs(readme): add usage for beta and next version (#243)

* chore: add js files to format script (#244)

* chore: remove credentials provider (#246)

* refactor(cli): add module path aliasing (#247)

* feat: updating tailwind and postcss config's to use .cjs (#242)

Co-authored-by: Shoubhit Dash <[email protected]>
Co-authored-by: Julius Marminge <[email protected]>

* fix: clarify some comments and rename some files in env (#245)

* fix: clarify some comments and rename some files in env

* fix: filenames in installer

* fix: import in next.config

* fix: import after filechange

* fix: forgotten rename

Co-authored-by: Shoubhit Dash <[email protected]>

* chore(release): bump beta version

* fix: added JSDoc type to clientEnv in all env-schema (#240)

* chore(release): 5.3.0

* refactor(utils): remove unnecessary block (#252)

* chore: update favicon (#253)

* fix: missing next-auth types (#255)

fixes #254

* fix: height on small screens (#256)

* docs: expand t3-app README (#248)

* revert: extend regular next-auth module (#257)

* revert: extend regular next-auth module

* refactor: move typedefs to `src/types`

Co-authored-by: Shoubhit Dash <[email protected]>

* feat: add deployment strategy to generated README (#258)

* fix(docs): remove extra s in link

* feat: add deployment strategy to readme

* docs: add instructions on building docker image to readme (#265)

* Add instructions on building docker image

Following the same method given in NextJs examples

* fix: prettier check

run prettier format to fix formatting issues

* docs: small adjustments (#266)

* docs: add instructions on building docker image to readme (#265)

* Add instructions on building docker image

Following the same method given in NextJs examples

* fix: prettier check

run prettier format to fix formatting issues

* docs: small adjustments

* feat: set appName to directory on 'npx create-t3-app .' (#273)

* fix(prisma): make db changes for next-auth on mysql obvious (#275)

* chore(release): 5.4.0

* feat: make session.user.id non-nullable (#282)

* chore(release): 5.5.0

* docs: update tRPC v10 docs link (#291)

* fix: change ! reversion to equality in env validation (#293)

* feat: Include ct3a version as metadata in generated app (#305)

* fix: parse path returned from process.cwd() before using it (#303)

Co-authored-by: Shoubhit Dash <[email protected]>
Co-authored-by: Julius Marminge <[email protected]>

* chore(release): 5.6.0

* chore: upload T3 logo to github (#311)

* chore: upload T3 logo to github

* chore: format

* chore: ignore package-lock (#315)

* chore: Publish Workflow and other Github enhancements (#308)

* chore: switch standard-version to changeset

* chore: add version workflow

* chore: issue forms and pr labeler

* chore: prettier

* chore: add missing credit

* chore: adjust some commands

* chore: update contributing guidelines

* chore: prettier

* chore: adjustments

* feat: add minimal ts-linter

* fix: lint warnings

* revert eslinter

* chore(ci): dont use reserved word `package`

* chore(ci): require -> import

* chore(ci): authenticate to npm

* chore: update pr-template

* chore(ci): next-release

* chore(ci): release workflow

* chore(ci): update scrips

* chore(ci): update release versioning

* chore(ci): update versioning scripts

* chore(ci): fix no-bin error

* feat: add base tslint (#317)

* chore(app): version bumps

* feat: add base linter for ts

* fix: test ignoring base

* fix: test setting root

* fix: test add parser

* fix: test disabling exclude

* chore: changeset

* chore(ci): test changeset with npx

* chore(ci): test npm

* chore(ci): give proper commit message and title

* chore(release): remove standard-version msg from changelog

* chore(release): version packages (#319)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* chore(release): public access

* refactor: turborepo with cli and www (#320)

* docs: add docs site built with Astro (#300)

* docs: add docs site built with Astro

* docs: prettier

Co-authored-by: Shoubhit Dash <[email protected]>

* refactor: make monorepo

* chore(fix): tailwindcss/typography peer deps

* refactor: docusaurus migration

* docs: styling

* fix: linting

* fix: typecheck

* fix: no typecheck www

* fix: restore tsconfig

* fix(ci): cache all node_modules

* fix: more caching

* fix: missing lint plugin

* fix(ci): cwd

* fix(ci): path

* docs: start on collection

* docs: headers

Co-authored-by: Kroucher <[email protected]>
Co-authored-by: Shoubhit Dash <[email protected]>
Co-authored-by: sir-mr-bean <[email protected]>

* chore: set type module

* fix: typo in pub-script

* fix: create npmrc in cli pkg

* fix: path in version script

* refactor: turborepo next

Co-authored-by: Oscar Chic <[email protected]>
Co-authored-by: Daniel Roe <[email protected]>
Co-authored-by: JLN13X <[email protected]>
Co-authored-by: Hauke Schnau <[email protected]>
Co-authored-by: Azmi Makarima <[email protected]>
Co-authored-by: Christopher Ehrlich <[email protected]>
Co-authored-by: Shoubhit Dash <[email protected]>
Co-authored-by: Nicholas Ramkissoon <[email protected]>
Co-authored-by: Vikrant Bhat <[email protected]>
Co-authored-by: Guillermo Antony Cava Nuñez <[email protected]>
Co-authored-by: Abui <[email protected]>
Co-authored-by: John Daly <[email protected]>
Co-authored-by: Simon Green Kristensen <[email protected]>
Co-authored-by: XLS <[email protected]>
Co-authored-by: Omar López <[email protected]>
Co-authored-by: Krish <[email protected]>
Co-authored-by: cyremur <[email protected]>
Co-authored-by: Samuel Gunter <[email protected]>
Co-authored-by: Khalil Omer <[email protected]>
Co-authored-by: BWsix <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Kroucher <[email protected]>
Co-authored-by: sir-mr-bean <[email protected]>
devvianto605 pushed a commit to devvianto605/create-devviantex-nextjs-app-deprecated that referenced this issue Jun 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 confirmed bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants