-
Notifications
You must be signed in to change notification settings - Fork 12.8k
TypeScript erroneously type checks original .ts
source files of libraries that have tsconfig.json#compilerOptions.declarationMap
#44522
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
Comments
TypeScript is checking the .ts files because they are included in your project, the node resolver roughly resolves as You don't have |
For example: node_modules/vite-plugin-ssr/utils/assert.ts:1:25 - error TS2732: Cannot find module '../package.json'. Consider using '--resolveJsonModule' to import module with '.json' extension.
1 import { version } from '../package.json'
~~~~~~~~~~~~~~~~~ The So it seems like TypeScript doesn't know how to map the |
Following the resolution strategy TS looks at your package.json then sees no You can look at either using |
I just tried your suggestions but without success.
When I set
I created an empty file
I'm not sure this is actionable: there are actually no |
You can use ======== Resolving module 'vite-plugin-ssr/plugin' from '/Users/ortatherox/dev/typescript/repros/vite-ssr-project/vite.config.ts'. ========
Module resolution kind is not specified, using 'NodeJs'.
Loading module 'vite-plugin-ssr/plugin' from 'node_modules' folder, target file type 'TypeScript'.
File '/Users/ortatherox/dev/typescript/repros/vite-ssr-project/node_modules/vite-plugin-ssr/plugin/package.json' does not exist.
Found 'package.json' at '/Users/ortatherox/dev/typescript/repros/vite-ssr-project/node_modules/vite-plugin-ssr/package.json'.
'package.json' does not have a 'typesVersions' field.
File '/Users/ortatherox/dev/typescript/repros/vite-ssr-project/node_modules/vite-plugin-ssr/plugin.ts' does not exist.
File '/Users/ortatherox/dev/typescript/repros/vite-ssr-project/node_modules/vite-plugin-ssr/plugin.tsx' does not exist.
File '/Users/ortatherox/dev/typescript/repros/vite-ssr-project/node_modules/vite-plugin-ssr/plugin.d.ts' does not exist.
'package.json' does not have a 'typings' field.
'package.json' does not have a 'types' field.
'package.json' has 'main' field './dist/cjs/index.js' that references '/Users/ortatherox/dev/typescript/repros/vite-ssr-project/node_modules/vite-plugin-ssr/plugin/dist/cjs/index.js'.
Loading module as file / folder, candidate module location '/Users/ortatherox/dev/typescript/repros/vite-ssr-project/node_modules/vite-plugin-ssr/plugin/dist/cjs/index.js', target file type 'TypeScript'.
File name '/Users/ortatherox/dev/typescript/repros/vite-ssr-project/node_modules/vite-plugin-ssr/plugin/dist/cjs/index.js' has a '.js' extension - stripping it.
File '/Users/ortatherox/dev/typescript/repros/vite-ssr-project/node_modules/vite-plugin-ssr/plugin/index.ts' exist - use it as a name resolution result.
Resolving real path for '/Users/ortatherox/dev/typescript/repros/vite-ssr-project/node_modules/vite-plugin-ssr/plugin/index.ts', result '/Users/ortatherox/dev/typescript/repros/vite-ssr-project/node_modules/vite-plugin-ssr/plugin/index.ts'.
======== Module name 'vite-plugin-ssr/plugin' was successfully resolved to '/Users/ortatherox/dev/typescript/repros/vite-ssr-project/node_modules/vite-plugin-ssr/plugin/index.ts' with Package ID 'vite-plugin-ssr/plugin/[email protected]'. ======== TypesWhat feels off to me is that using
Because it looks for
and not
which seems unintuitive to me, and maybe wrong - but it does feel strange that this behavior has always existed the TS node resolution maybe we're not resolving the |
Hm, that does seem strange indeed. I wouldn't know why it would make sense to consider Also what I find strange is that:
It says that So I guess there is a TS bug going on here? |
Yeah, I think so, perhaps we've not encountered a case where both
|
Actually, TypeScript's behavior matches node's, this isn't a bug with TypeScript's node resolver: β― mkdir 44522
β― cd 44522
β― mkdir -p node_modules/a/dist
β― touch node_modules/a/dist/plugins.js
β― touch node_modules/a/package.json
β― cat node_modules/a/package.json
{
"main":"dist"
}
β― node
> require.resolve("a/plugins")
Uncaught Error: Cannot find module 'a/plugins'
Require stack:
- <repl>
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:924:15)
at Function.resolve (node:internal/modules/cjs/helpers:98:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '<repl>' ]
} The private access in TS' type resolution doesn't respect the Which means TypeScript resolving to the root |
It uses
Maybe TS doesn't take in consideration |
TypeScript does not, TypeScript still needs to support older node's and this sort of behavior will need to be opt in.Maybe 4.4 will get it, little bit hard to tell yet as there's a lot to cover with node's new ESM resolvers #44442 |
Ok neat. Thanks for the conversation. I'm closing this since the issue is about ESM resolving. |
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
π Search Terms
Library
declarationMap
Declaration Map
π Version & Regression Information
This happens with
[email protected]
but I believe it to happen with prior versions as well.β― Playground Link
π Actual behavior
TypeScript type checks the original
.ts
files of vite-plugin-ssr.π Expected behavior
TypeScript should only type check the declaration
.d.ts
files.Note that
vite-plugin-ssr
doesn't define apackage.json#types
but settingpackage.json#types
doesn't solve the problem.Overall goal here is to enable
vite-plugin-ssr
users to browse the orginal.ts
source files ofvite-plugin-ssr
. It's currenlty not a viable option because TypeScript type checks the original.ts
source files ofvite-plugin-ssr
which leads to errors when the user'stsconfig.json
mismatchesvite-pluin-ssr
'stsconfig.json
. For example,vite-plugin-ssr
needstsconfig.json#compilerOptions.resolveJsonModule
set totrue
and building the user's TypeScript project fails if the user didn't set that flag.The text was updated successfully, but these errors were encountered: