microsoft / microsoft/TypeScript

Emit .d.ts for type-only usage of .d.ts files in a project

Open
#38,146 0 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Proposal Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms

type only export import declaration declarations .d.ts 3.8

Suggestion

When building code has .d.ts files written and used internally, the declarations aren't emitted. This makes sense currently because .d.ts files aren't compiled. However, it causes typechecking errors when exporting content from the package for external usage, since the declaration isn't included in the compiled output.

import/export type made some initial progress for me by removing the types from the compiled JS output. but I believe it would be even more useful to re emit declarations for type only import exports. this would ensure that ambient declarations are included in the final compiled output.

My issue may be incorrect usage of declaration files, which I've only been using due to the external code generation using it. I'm not sure I explained my idea clearly, but I believe my below examples should cover it - please follow up if not.

Use Cases

I have an existing project that exports declarations generated by an external package from non-JS code. The original code doesn't need to be included, just the types for usage by external consumers. When I want to reexport those declarations, I have to manually copy them over to the build folder after since they disappear. You can see an example of this here

Examples

given directory structure

./src/index.ts
./src/generated.d.ts

and usage

// ./src/index.ts
import type Foo from './generated.d.ts'

the output structure should be

./dist/index.js
./dist/index.d.ts
./dist/generated.d.ts

Checklist

My suggestion meets these guidelines:

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source files or tests are named. Start by tracing TypeScript's declaration-emission handling for type-only imports and .d.ts inputs, then compare it with the requested src-to-dist example; done means generated output includes the referenced declaration without changing JavaScript output.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.