Skip to content

Fix for dts emit for tripple slash directives in d.ts files #1519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4403,6 +4403,9 @@ func (p *Printer) emitSourceFile(node *ast.SourceFile) {
p.emitShebangIfNeeded(node)
index = p.emitPrologueDirectives(node.Statements)
p.emitHelpers(node.AsNode())
if node.IsDeclarationFile {
p.emitTripleSlashDirectives(node)
}
}

// !!! Emit triple-slash directives
Expand All @@ -4420,6 +4423,23 @@ func (p *Printer) emitSourceFile(node *ast.SourceFile) {
p.commentsDisabled = savedCommentsDisabled
}

func (p *Printer) emitTripleSlashDirectives(node *ast.SourceFile) {
p.emitDirective("path", node.ReferencedFiles)
p.emitDirective("types", node.TypeReferenceDirectives)
p.emitDirective("lib", node.LibReferenceDirectives)
}

func (p *Printer) emitDirective(kind string, refs []*ast.FileReference) {
for _, ref := range refs {
var resolutionMode string
if ref.ResolutionMode != core.ResolutionModeNone {
resolutionMode = fmt.Sprintf(`resolution-mode="%s" `, core.IfElse(ref.ResolutionMode == core.ResolutionModeESM, "import", "require"))
}
p.writeComment(fmt.Sprintf("/// <reference %s=\"%s\" %s%s/>", kind, ref.FileName, resolutionMode, core.IfElse(ref.Preserve, `preserve="true" `, "")))
p.writeLine()
}
}

//
// Lists
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ foo_1.x + bar_1.y;
//# sourceMappingURL=../myMapRoot/index.js.map

//// [/app/bin/index.d.ts]
/// <reference path="../../types/bar.d.ts" preserve="true" />
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,3 @@
+const bar_1 = require("bar");
foo_1.x + bar_1.y;
//# sourceMappingURL=../myMapRoot/index.js.map

//// [/app/bin/index.d.ts]
-/// <reference path="../../types/bar.d.ts" preserve="true" />
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ exports.x = y;
//# sourceMappingURL=../src/myMapRoot/index.js.map

//// [/app/bin/index.d.ts]
/// <reference path="../lib/bar.d.ts" preserve="true" />
export declare const x: number;

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,6 @@ declare module "SubModule" {
}
}
//// [declFileAmbientExternalModuleWithSingleExportedModule_1.d.ts]
/// <reference path="declFileAmbientExternalModuleWithSingleExportedModule_0.d.ts" preserve="true" />
import SubModule = require('SubModule');
export declare var x: SubModule.m.m3.c;


//// [DtsFileErrors]


declFileAmbientExternalModuleWithSingleExportedModule_1.d.ts(1,28): error TS2307: Cannot find module 'SubModule' or its corresponding type declarations.


==== declFileAmbientExternalModuleWithSingleExportedModule_1.d.ts (1 errors) ====
import SubModule = require('SubModule');
~~~~~~~~~~~
!!! error TS2307: Cannot find module 'SubModule' or its corresponding type declarations.
export declare var x: SubModule.m.m3.c;

==== declFileAmbientExternalModuleWithSingleExportedModule_0.d.ts (0 errors) ====
declare module "SubModule" {
namespace m {
namespace m3 {
interface c {
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ var x = new M.C(); // Declaration file wont get emitted because there are errors


//// [client.d.ts]
/// <reference path="declFile.d.ts" preserve="true" />
declare var x: M.C; // Declaration file wont get emitted because there are errors in declaration file
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
--- old.declFileWithErrorsInInputDeclarationFile.js
+++ new.declFileWithErrorsInInputDeclarationFile.js
@@= skipped -20, +20 lines =@@

@@= skipped -21, +21 lines =@@

//// [client.d.ts]
-/// <reference path="declFile.d.ts" preserve="true" />
/// <reference path="declFile.d.ts" preserve="true" />
-declare var x: M.C;
+declare var x: M.C; // Declaration file wont get emitted because there are errors in declaration file
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,5 @@ export async function drainStream(stream: NodeJS.ReadableStream): Promise<void>


//// [app.d.mts]
/// <reference types="node" preserve="true" />
export declare function drainStream(stream: NodeJS.ReadableStream): Promise<void>;


//// [DtsFileErrors]


/app.d.mts(1,45): error TS2503: Cannot find namespace 'NodeJS'.


==== /tsconfig.json (0 errors) ====
{
"compilerOptions": {
"module": "nodenext",
"types": [],
"declaration": true,
"emitDeclarationOnly": true,
}
}

==== /app.d.mts (1 errors) ====
export declare function drainStream(stream: NodeJS.ReadableStream): Promise<void>;
~~~~~~
!!! error TS2503: Cannot find namespace 'NodeJS'.

==== /node_modules/@types/node/package.json (0 errors) ====
{
"name": "@types/node",
"version": "1.0.0",
"types": "index.d.ts"
}

==== /node_modules/@types/node/globals.d.ts (0 errors) ====
declare namespace NodeJS {
interface ReadableStream {}
}

==== /node_modules/@types/node/index.d.ts (0 errors) ====
/// <reference path="globals.d.ts" />

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ declare namespace My.Internal {
}
}
//// [usage.d.ts]
/// <reference path="internal.d.ts" preserve="true" />
declare namespace SomeOther.Thing {
class Foo {
private _which;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,4 @@
+ Internal.WhichThing.A ? "foo" : "bar";
}
}
Thing.Foo = Foo;
@@= skipped -34, +41 lines =@@
}
}
//// [usage.d.ts]
-/// <reference path="internal.d.ts" preserve="true" />
declare namespace SomeOther.Thing {
class Foo {
private _which;
Thing.Foo = Foo;
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,8 @@ export default Foo;


//// [jsxDeclarationsWithEsModuleInteropNoCrash.d.ts]
/// <reference path="..react16.d.ts" preserve="true" />
declare function Foo({ bar }: {
bar: any;
}): JSX.Element;
export default Foo;


//// [DtsFileErrors]


jsxDeclarationsWithEsModuleInteropNoCrash.d.ts(3,5): error TS2503: Cannot find namespace 'JSX'.


==== jsxDeclarationsWithEsModuleInteropNoCrash.d.ts (1 errors) ====
declare function Foo({ bar }: {
bar: any;
}): JSX.Element;
~~~
!!! error TS2503: Cannot find namespace 'JSX'.
export default Foo;

Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
--- old.jsxDeclarationsWithEsModuleInteropNoCrash.js
+++ new.jsxDeclarationsWithEsModuleInteropNoCrash.js
@@= skipped -24, +24 lines =@@

@@= skipped -25, +25 lines =@@

//// [jsxDeclarationsWithEsModuleInteropNoCrash.d.ts]
-/// <reference path="..react16.d.ts" preserve="true" />
/// <reference path="..react16.d.ts" preserve="true" />
-export default Foo;
declare function Foo({ bar }: {
bar: any;
Expand All @@ -21,20 +20,4 @@
- export { bar_1 as bar };
-}
-import PropTypes from 'prop-types';
+export default Foo;
+
+
+//// [DtsFileErrors]
+
+
+jsxDeclarationsWithEsModuleInteropNoCrash.d.ts(3,5): error TS2503: Cannot find namespace 'JSX'.
+
+
+==== jsxDeclarationsWithEsModuleInteropNoCrash.d.ts (1 errors) ====
+ declare function Foo({ bar }: {
+ bar: any;
+ }): JSX.Element;
+ ~~~
+!!! error TS2503: Cannot find namespace 'JSX'.
+ export default Foo;
+
+export default Foo;
Original file line number Diff line number Diff line change
Expand Up @@ -43,39 +43,11 @@ declare module "SubModule" {
export = SubModule;
}
//// [missingImportAfterModuleImport_1.d.ts]
/// <reference path="missingImportAfterModuleImport_0.d.ts" preserve="true" />
import SubModule = require('SubModule');
declare class MainModule {
// public static SubModule: SubModule;
SubModule: SubModule;
constructor();
}
export = MainModule;


//// [DtsFileErrors]


missingImportAfterModuleImport_1.d.ts(1,28): error TS2307: Cannot find module 'SubModule' or its corresponding type declarations.


==== missingImportAfterModuleImport_1.d.ts (1 errors) ====
import SubModule = require('SubModule');
~~~~~~~~~~~
!!! error TS2307: Cannot find module 'SubModule' or its corresponding type declarations.
declare class MainModule {
// public static SubModule: SubModule;
SubModule: SubModule;
constructor();
}
export = MainModule;

==== missingImportAfterModuleImport_0.d.ts (0 errors) ====
declare module "SubModule" {
class SubModule {
static StaticVar: number;
InstanceVar: number;
constructor();
}
export = SubModule;
}

Loading
Loading