parcel icon indicating copy to clipboard operation
parcel copied to clipboard

Type-declaration files (.d.ts) invalid in build output

Open mupperton opened this issue 3 years ago β€’ 1 comments

πŸ› bug report

Have a src folder that contains:

  • types.d.ts for custom types
  • main.ts that imports one of those types

Tell parcel to create a type declaration file in the output with the following config

  // package.json
  "types": "dist/types.d.ts",

Run parcel build and the generated declaration file will try to import the declaration file from your src folder with a broken relative link The relative link also appears to be coupled to the relativity of how the source code imports the original declaration file

e.g.

src
β”‚   types.d.ts
β”‚   main.ts    

will result in a import { XYZ } from './types', and

src
β”‚   types.d.ts    
β”‚
└───folder
β”‚   β”‚   main.ts

will result in a import { XYZ } from '../types' (because the main.ts would also import types from the parent directory)

πŸŽ› Configuration

I've created a simple reproduction in the below repo https://github.com/mupperton/parcel-types-issue (README explains how to run) with configuration here https://github.com/mupperton/parcel-types-issue/blob/main/package.json

πŸ€” Expected Behavior

Either the import from the dist folder knows to import from the src folder, so would result in import { XYZ } from '../src/types' or The types are completely re-declared in full in the dist folder - especially useful for npm packages that don't include the src directory as part of the package (because then even a valid relative path at build time would not resolve once published)

😯 Current Behavior

A relative import path is used which doesn't resolve to any real file

πŸ’ Possible Solution

I'm personally new to Parcel and not even a TS wizard, so not sure where to start, this even may end up being a TS issue rather than Parcel itself...

πŸ”¦ Context

Unable to output a valid type declaration file to be used by consumers of the npm package

A workaround atm is to define the types in the same file as the code (so in main.ts for example) as then the types get completely redeclared in the build output. However, I'd like to share type declarations across some code files, so extracting them into a declaration file is preferred...

πŸ’» Code Sample

As provided above: https://github.com/mupperton/parcel-types-issue (README explains how to run)

🌍 Your Environment

Software Version(s)
Parcel 2.3.2
Node 16
npm/Yarn 8
Operating System macos 10.15.7

Also same results with another device and setup

Software Version(s)
Parcel 2.3.2
Node 14
npm/Yarn 6
Operating System macos 12.2.1

mupperton avatar Mar 03 '22 20:03 mupperton

Hey! - I had this same problem and I confirm this is still an issue on [email protected].

A workaround I have found is to rename .d.ts files into .d.tsx files. πŸŽ‰

This change generates types correctly on bundler. Although, I haven't gone deep enough on parcel source code to figure out why this change on naming make it work.

Hope it helps!

AlberErre avatar Sep 13 '22 13:09 AlberErre

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs.

github-actions[bot] avatar Mar 22 '24 12:03 github-actions[bot]