Title: Module resolution for sub-packages picks d.ts file when .ts file is available · Issue #20248 · microsoft/TypeScript · GitHub
Open Graph Title: Module resolution for sub-packages picks d.ts file when .ts file is available · Issue #20248 · microsoft/TypeScript
X Title: Module resolution for sub-packages picks d.ts file when .ts file is available · Issue #20248 · microsoft/TypeScript
Description: 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 direct...
Open Graph Description: 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, ...
X Description: 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, ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/20248
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Module resolution for sub-packages picks d.ts file when .ts file is available","articleBody":"\u003c!-- BUGS: Please use this template. -->\r\n\u003c!-- QUESTIONS: This is not a general support forum! Ask Qs at http://stackoverflow.com/questions/tagged/typescript -->\r\n\u003c!-- SUGGESTIONS: See https://github.com/Microsoft/TypeScript-wiki/blob/master/Writing-Good-Design-Proposals.md -->\r\nWhen 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.\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 2.7.0-dev.20171124 (also older versions)\r\n\r\n**Code**\r\n\r\n*subPackage/package.json*\r\n```json\r\n{\r\n \"name\": \"subpackage\",\r\n \"version\": \"0.0.1\",\r\n \"main\": \"./index.js\",\r\n \"types\": \"./index.d.ts\"\r\n}\r\n```\r\n\r\n*subPackage/index.ts*\r\n```ts\r\nexport interface Utility {\r\n utilityFunction(): void;\r\n}\r\n\r\nexport const utility: Utility = {\r\n utilityFunction: () => { console.log('utility function'); }\r\n}\r\n```\r\n\r\n*index.ts*\r\n```ts\r\nimport { utility } from './subPackage';\r\n\r\nutility.utilityFunction();\r\n```\r\n\r\n*$ tsc --declaration --traceresolution (this will work on the first run, and fail on the second)*\r\n```\r\n======== Resolving module './subPackage' from 'package/index.ts'. ========\r\nExplicitly specified module resolution kind: 'NodeJs'.\r\nLoading module as file / folder, candidate module location 'package/subPackage', target file type 'TypeScript'.\r\nFile 'package/subPackage.ts' does not exist.\r\nFile 'package/subPackage.tsx' does not exist.\r\nFile 'package/subPackage.d.ts' does not exist.\r\nFound 'package.json' at 'package/subPackage/package.json'.\r\n'package.json' does not have a 'typings' field.\r\n'package.json' has 'types' field './index.d.ts' that references 'package/subPackage/index.d.ts'.\r\nFile 'package/subPackage/index.d.ts' exist - use it as a name resolution result.\r\n======== Module name './subPackage' was successfully resolved to 'package/subPackage/index.d.ts'. ========\r\nerror TS5055: Cannot write file 'package/subPackage/index.d.ts' because it would overwrite input file.\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nWhen 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.\r\n\r\n**Actual behavior:**\r\n\r\nWhen 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.\r\n","author":{"url":"https://github.com/jeffora","@type":"Person","name":"jeffora"},"datePublished":"2017-11-24T06:42:20Z","url":"https://github.com/microsoft/TypeScript/issues/20248"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:bd629169-426c-d9eb-9c7e-1411bffa2f45 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A6A4:23CF06:2C18BF8:3829B49:6B01A2B5 |
| html-safe-nonce | 57aaf9e21986b3e5eadabe2c1e353f187405342ab5023653f7ff8c3e898b3b3b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNkE0OjIzQ0YwNjoyQzE4QkY4OjM4MjlCNDk6NkIwMUEyQjUiLCJ2aXNpdG9yX2lkIjoiNzU0MDMzNzMyNzg4MTY5MTgyOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b31753b631046659a07ea1fc9174873dd94e7a75d7152ea051eb5bc772059a2a |
| hovercard-subject-tag | issue:276527023 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/20248/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ebf4314a27fbb5f7b09c386980868b7487a398e3710026d3da470baef1cd2d93/microsoft/TypeScript/issues/20248 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ebf4314a27fbb5f7b09c386980868b7487a398e3710026d3da470baef1cd2d93/microsoft/TypeScript/issues/20248 |
| og:image:alt | 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, ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jeffora |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width