docs(declaration-files): document implicit export visibility in .d.ts modules#3615
Open
milcho0604 wants to merge 1 commit into
Open
docs(declaration-files): document implicit export visibility in .d.ts modules#3615milcho0604 wants to merge 1 commit into
milcho0604 wants to merge 1 commit into
Conversation
… modules
All top-level declarations in a module .d.ts file (types and values) are
importable from outside even without the export keyword, and appending
'export {}' opts back into only-explicit-exports visibility. This behavior
is by design but was not covered in the handbook.
Refs microsoft/TypeScript#32182
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a section to the declaration-files Deep Dive page documenting a
long-standing but undocumented behavior: in a module
.d.tsfile, alltop-level declarations (types and values) are importable from outside the
module even without the
exportkeyword, and appendingexport {}opts backinto normal only-explicit-exports visibility.
This is the behavior discussed in microsoft/TypeScript#32182 (open since 2019,
labeled
Docs), where it was noted that the handbook doesn't mention it.Why
I hit this while adding a type-export test to a library PR: a compile-only
test cannot assert the
exportkeyword on a.d.tstype, because removingexportstill type-checks for consumers. The behavior is by design, butnothing in the handbook says so — the Deep Dive page seemed like the natural
home, since it already covers declaration-file name resolution.
All claims verified against TypeScript 5.9.3:
.tsmodule, import non-exported type.d.tsmodule, import non-exported type.d.tsmodule, import non-exporteddeclare const/declare class.d.tswithexport {}addedThe runtime caution was also verified on Node.js: importing a binding that the
underlying JS module doesn't export throws at load time under ESM and yields
undefinedunder CommonJS interop.Notes
foo.d.ts… Then consumed it:").Refs microsoft/TypeScript#32182