首页 > 编程问答 >如何从“.d.ts”文件生成 TypeScript API 文档?

如何从“.d.ts”文件生成 TypeScript API 文档?

时间:2024-07-20 17:39:39浏览次数:8  
标签:typescript typedoc api-documentation

如何从 .d.ts 文件生成typescript API文档?

我尝试 typedoc 传输 这个库

% npx typedoc index.js 
[warning] The entry point ./index.js is not referenced by the 'files' or 'include' option in your tsconfig
[error] Unable to find any entry points. See previous warnings
[error] Found 1 errors and 1 warnings

并且也不能直接从 .d.ts 文件传输:

% npx typedoc tree-sitter.d.ts 
tree-sitter.d.ts:187:51 - error TS2580: Cannot find name 'Buffer'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.

187       constructor(language: any, source: string | Buffer);
                                                      ~~~~~~

tree-sitter.d.ts:206:7 - error TS1165: A computed property name in an ambient context must refer to an expression whose type is a literal type or a 'unique symbol' type.

206       [Symbol.iterator](): Iterator<string>;
          ~~~~~~~~~~~~~~~~~

tree-sitter.d.ts:206:8 - error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the 'lib' compiler option to es2015 or later.

206       [Symbol.iterator](): Iterator<string>;
           ~~~~~~

tree-sitter.d.ts:206:28 - error TS2583: Cannot find name 'Iterator'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.

206       [Symbol.iterator](): Iterator<string>;
                               ~~~~~~~~

[error] Found 4 errors and 0 warnings

You're getting those errors because Typedoc needs a JavaScript/TypeScript context to understand the types in your .d.ts file. Here's how to generate documentation from .d.ts files using Typedoc:

1. Create a minimal TypeScript configuration file ( tsconfig.json ):

{
  "compilerOptions": {
    "target": "es2015",
    "lib": ["es2015"],
    "types": ["node"] 
  },
  "include": ["tree-sitter.d.ts"] 
}

This configuration tells Typedoc:

  • target and lib : To use the ES2015 features required by your .d.ts file (like Symbol.iterator ).
  • types : To include type definitions for Node.js, resolving the Buffer and Iterator errors.
  • include : To specifically include your .d.ts file for processing.

2. Run Typedoc:

npx typedoc --tsconfig tsconfig.json

By specifying the --tsconfig flag, Typedoc will use the settings from your tsconfig.json file, resolving the type dependencies and generating the documentation.

Additional Tips:

  • Customize Typedoc Output: Refer to the Typedoc documentation ( https://typedoc.org/ ) for customization options like output format (HTML, Markdown, JSON), themes, and more.
  • No index.js Entry Point: Since you're documenting types from a .d.ts file, you don't need to specify an entry point like index.js .
  • Keep tsconfig.json Minimal: Include only the necessary settings in your tsconfig.json to avoid conflicts if you have other TypeScript projects.

This approach should generate your TypeScript API documentation correctly from the .d.ts file.

标签:typescript,typedoc,api-documentation
From: 78772395

相关文章

  • TS 入门(七):TypeScript模块与命名空间
    目录前言回顾泛型编程1.模块a.导入和导出b.默认导出c.重命名导入和导出2.命名空间a.定义命名空间b.嵌套命名空间3.动态导入与条件导入a.动态导入b.条件导入结语前言在前几章中,我们学习了TypeScript的基础知识、函数与对象类型、接口与类、以及泛型编......
  • TypeScript
    TypeScriptTypeScript简介使用ts类型注解JS已有类型TS新增类型Typescript高级类型TypeScript简介1.ts是js的超集,在js的基础上,添加了类型支持(:表示)2.ts属于静态类型(编译期做类型检查)的编程语言,减少不同类型的赋值错误(意外行为),js属于动态类型(执行期进行类型检查)3.......
  • TS 入门(四):TypeScript 高级类型与类型操作
    目录前言回顾1.联合类型和交叉类型a.联合类型b.交叉类型2.字面量类型3.类型断言4.类型兼容性5.类型守卫a.`typeof`类型守卫b.`instanceof`类型守卫c.自定义类型守卫扩展知识点:类型映射(MappedTypes)结语前言在前三章中,我们介绍了TypeScript的基础知识......
  • Typescript 中 interface 和 type 的区别
    在TypeScript中,interface​和type​是用来描述对象结构或类型的两种主要方式,它们有一些区别和各自的特点。Interface(接口)定义方式:使用interface​关键字定义,例如:interfacePerson{name:string;age:number;}适用场景:主要用于描述对象的形状(Sh......
  • TS 入门(五):TypeScript接口与类
    目录前言回顾高级类型与类型操作1.接口a.基本接口b.可选属性和只读属性c.函数类型接口d.可索引类型e.接口继承2.类a.基本类b.类的成员(属性和方法)c.构造函数d.继承和派生类e.公有、私有和受保护的修饰符f.静态属性和方法g.抽象类扩展知识点:接口与类的结合......
  • vue3+element-plus+typescript
    1.vue3+ts+elementui-plushttps://blog.csdn.net/qq_41737571/article/details/1390730852.自动调整font-size大小https://blog.csdn.net/qq_41737571/article/details/1401586143.简单小众电商购物项目模板:基于Vue3和Vant4的纯前端开发方案https://web-hls.blog.csdn.......
  • vite react Typescript 构建一个移动端网页
    使用Vite、React和TypeScript来构建一个移动端网页是一个高效且现代的开发方式。Vite是一个构建工具和开发服务器,它利用原生ES模块导入来提供快速的冷启动和即时模块热更新(HMR)。React是用于构建用户界面的JavaScript库,而TypeScript是JavaScript的一个超集,它添......
  • TypeScript中的交叉类型
            交叉类型:将多个类型合并为一个类型,使用&符号连接。typeAProps={a:string}typeBProps={b:number}typeallProps=AProps&BPropsconstInfo:allProps={a:'小月月',b:7}        我们......
  • TypeScript的类型谓词与控制流分析
    目录ts封装类型判断的问题类型谓词TypeScript的“控制流分析”ts封装类型判断的问题在union.d.ts中全局声明一个DataTypedeclaretypeDataType=|"RegExp"|"Object"|"Array"|"Function"|"String"|"Boolean"|"......
  • TypeScript笔记(一)
    一、TypeScript=Type+JavaScript  在JS基础上,为JS增加了类型支持。TS属于静态类型的编程语言,在编译期间做类型检查,可以在代码编写期间发现问题,减少调试时间。TS相比JS的优势:1、更早的发现错误,减少调试时间;2、代码提示;3、提升可维护性;4、ECMAScript;5、TS有类型推断......