TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Entry point of ts files at the package.json level

Open StefanBrown opened this issue 3 years ago • 1 comments

Suggestion

🔍 Search Terms

import ts without precompilation

✅ Viability Checklist

My suggestion meets these guidelines:

  • [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [x] This wouldn't change the runtime behavior of existing JavaScript code
  • [x] This could be implemented without emitting different JS based on the types of the expressions
  • [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • [x] This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Support for importing ts files at the package level.

📃 Motivating Example

Adding typescript entry points ts-module and ts-import to package.json


{
  "version": "1.0.0",
  "name": "@scope/foo",
  "module": "dist/index.mjs",
  "main": "dist/index.js",
  "types": "types/index.d.ts",
  "ts-module": "src/index.ts",
  "exports": {
    ".": {
      "import": "./dist/index.mjs",
      "require": "./dist/index.js",
      "ts-import": "./src/index.ts"
    }
  }
}

💻 Use Cases

This will improve the developer experience. No need to compile files, work directly from source codes. The types folder with d.ts files is no longer needed during custom code development.

When developing packages in the workspace, for example: @scope/foo, @scope/bar. (each package is compiled separately) @scope/bar depends on @scope/foo.

index.ts in @scope/bar:

import { MyInterface } from '@scope/foo';  // !!!< scope/foo must be pre-compiled

Now, in order for this to work without compilation, and the import went straight from the source codes, you need to add the following construction to tsconfig.json:

"paths": {
  "@scope/foo": ["./foo/src/index"]
}

This approach has a problem after compiling @scope/bar with d.ts files. d.ts files appear in DeclarationDir that are needed for another package (@scope/foo)

StefanBrown avatar Mar 02 '22 14:03 StefanBrown

Is there any update on this? This will be a very great feature for monorepos and serverless projects.

germanamz avatar Sep 21 '22 16:09 germanamz