Typescript Compiler API: JsDocs skipped even when using ts.createPrinter({removeComments: false })

I’m trying to generate function declarations from methods of an interface. Although the function are correctly created their JsDocs is missing, even when I explicitly say to the printer not to remove them.

Here is a simplify version of my interface:

//api-interface.ts
export interface SourceInterface {
  /**
   * 
   * The source of the data.
   * 
   * @param label The label to be used as the source for the data.
   */
  source: <T>(label: T) => void;
}

I’m expecting to generate the following file:

//api.ts

/**
* 
* The source of the data.
* 
* @param label The label to be used as the source for the data.
*/
export declare function source<T>(label: T): void;

Here is my code to generate the api.ts file using the Typescript API (^5.3.3)

//generate-api.ts

import * as fs from 'node:fs';
import * as ts from 'typescript';

const interfaceFile = './api-interface.ts';
const sourceFile = ts.createSourceFile(
  interfaceFile,
  fs.readFileSync(interfaceFile, 'utf8'),
  ts.ScriptTarget.Latest,
  true,
  ts.ScriptKind.TS
);

// Function to generate function declarations from interface
function generateFunctionDeclarations(node: ts.Node): ts.FunctionDeclaration[] {
  if (!ts.isInterfaceDeclaration(node)) return [];

  const functionDeclarations = node.members.map(member => {
    if (ts.isPropertySignature(member) && member.type && ts.isFunctionTypeNode(member.type)) {
      const functionName = (member.name as ts.Identifier).text;
      const functionIdentifier = ts.factory.createIdentifier(functionName);
      const typeParameters = member.type.typeParameters;
      const parameters = member.type.parameters;
      const returnType = member.type.type;
      const jsDocTags = ts.getJSDocTags(member);
      const modifiers = [
        ts.factory.createModifier(ts.SyntaxKind.ExportKeyword),
        ts.factory.createModifier(ts.SyntaxKind.DeclareKeyword)
      ];

      const functionDeclaration = ts.factory.createFunctionDeclaration(
        modifiers,
        undefined,
        functionIdentifier,
        typeParameters,
        parameters,
        returnType,
        undefined
      );

      if ('jsDoc' in functionDeclaration) functionDeclaration.jsDoc = jsDocTags;

      return functionDeclaration;
    }
   return undefined;
  }).filter(value => value !== undefined);

  return functionDeclarations;
}

const functionDeclarations = sourceFile.statements.flatMap(generateFunctionDeclarations);
const apiFile = ts.createSourceFile('api.ts', '', ts.ScriptTarget.Latest, false, ts.ScriptKind.TS);
const result = ts.factory.updateSourceFile(apiFile, functionDeclarations);

const printer = ts.createPrinter({removeComments: false });
const resultText = printer.printFile(result);
fs.writeFileSync('api.ts', resultText);

Any idea what I’m missing?

Here is a TS Playground where I just read the interface code from a variable and print the result to console using the global ts object, which is available in the Playground

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật