You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When importing a file from a subfolder, where that folder also contains a package.json file with a types or typings entry, the module resolver will select the declaration file pointed to by types, instead of following normal local directory resolution and selecting an index.ts file.
$ tsc --declaration --traceresolution (this will work on the first run, and fail on the second)
======== Resolving module './subPackage' from 'package/index.ts'. ========
Explicitly specified module resolution kind: 'NodeJs'.
Loading module as file / folder, candidate module location 'package/subPackage', target file type 'TypeScript'.
File 'package/subPackage.ts' does not exist.
File 'package/subPackage.tsx' does not exist.
File 'package/subPackage.d.ts' does not exist.
Found 'package.json' at 'package/subPackage/package.json'.
'package.json' does not have a 'typings' field.
'package.json' has 'types' field './index.d.ts' that references 'package/subPackage/index.d.ts'.
File 'package/subPackage/index.d.ts' exist - use it as a name resolution result.
======== Module name './subPackage' was successfully resolved to 'package/subPackage/index.d.ts'. ========
error TS5055: Cannot write file 'package/subPackage/index.d.ts' because it would overwrite input file.
Expected behavior:
When the import path is a local folder (i.e. starts with a ./), and is not a package from node_modules, I would expect normal file resolution rules to take priority to try to import an index.ts file.
Actual behavior:
When no d.ts file exists, the compilation (with --declaration) succeeds. When exactly the same command is run again, it fails, as it now resolves the imported file as index.d.ts, and also tries to generate that file. Typescript will error trying to overwrite a file that was also an input to compilation.
The text was updated successfully, but these errors were encountered:
Thinking about this more I realise this is basically just mimicking the same behaviour as Node's module resolution, so I know it's not a simple issue or simple fix. However, it does seem problematic that the presence of a subpackage package.json with a types entry can cause different behaviour to exist between two subsequent calls to tsc --declaration.
Thinking about this more I realise this is basically just mimicking the same behaviour as Node's module resolution, so I know it's not a simple issue or simple fix.
That is what it is doing.. the compiler overlays TS-sepcific files on top of the node module resolution.
However, it does seem problematic that the presence of a subpackage package.json with a types entry can cause different behaviour to exist between two subsequent calls to tsc --declaration.
While i do agree in principal, i am not sure i see how it can be done otherwise. the process of resolving a module depends on finding a file in a set of prop-locations.. so if the file is not found it moves on to the next one..
the same happens for node "main" for instance, if that one was not found, it moves on to the next location, and looks for index.js for instance.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
When importing a file from a subfolder, where that folder also contains a
package.json
file with atypes
ortypings
entry, the module resolver will select the declaration file pointed to bytypes
, instead of following normal local directory resolution and selecting anindex.ts
file.TypeScript Version: 2.7.0-dev.20171124 (also older versions)
Code
subPackage/package.json
subPackage/index.ts
index.ts
$ tsc --declaration --traceresolution (this will work on the first run, and fail on the second)
Expected behavior:
When the import path is a local folder (i.e. starts with a
./
), and is not a package from node_modules, I would expect normal file resolution rules to take priority to try to import anindex.ts
file.Actual behavior:
When no
d.ts
file exists, the compilation (with--declaration
) succeeds. When exactly the same command is run again, it fails, as it now resolves the imported file asindex.d.ts
, and also tries to generate that file. Typescript will error trying to overwrite a file that was also an input to compilation.The text was updated successfully, but these errors were encountered: