# js2dt

> a lib for js generate d.ts with jsdoc

Latest version **0.2.5** (published 2020-07-17) · ISC license · 0 weekly downloads

## Install

```sh
npm install js2dt
pnpm add js2dt
yarn add js2dt
bun add js2dt
```

Provides the command `js2dt`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.5 |
| Published | 2020-07-17 |
| First published | 2020-07-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 45.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | demonskp |
| Maintainers | demonskp |
| Keywords | JavaScript, TypeScript, d.ts, jsdoc |

## Links

- npm: https://www.npmjs.com/package/js2dt
- npm.io page: https://npm.io/package/js2dt

## Dependencies (6)

- [mkdirp](https://npm.io/package/mkdirp.md) ^1.0.4
- [commander](https://npm.io/package/commander.md) ^5.1.0
- [@babel/types](https://npm.io/package/@babel/types.md) ^7.10.4
- [@babel/parser](https://npm.io/package/@babel/parser.md) ^7.10.2
- [@babel/traverse](https://npm.io/package/@babel/traverse.md) ^7.10.4
- [@babel/generator](https://npm.io/package/@babel/generator.md) ^7.10.4

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.2.5 (latest) — 2020-07-17
- 0.2.4 — 2020-07-16
- 0.1.4 — 2020-07-16
- 0.1.3 — 2020-07-15
- 0.1.2 — 2020-07-13
- 0.1.1 — 2020-07-13
- 0.0.7 — 2020-07-13
- 0.0.6 — 2020-07-07
- 0.0.5 — 2020-07-07
- 0.0.4 — 2020-07-03
- 0.0.3 — 2020-07-03
- 0.0.2 — 2020-07-01
- 0.0.1 — 2020-07-01

## README

# js2dt

[github](https://github.com/demonskp/js2dt)  |  [npm](https://www.npmjs.com/package/js2dt)

通过AST语法树和jsdoc注释解析将JavaScript代码转换成对应的Typescript的Type文件，也就是.js转.d.ts文件。由于js和ts的语法区别，并不保证可以完美转换。

## 状态

status：Alpha

## 安装

通过npm全局安装：

```
npm install -g js2dt
```

作为babel插件本地安装：

```
npm install --save-dev js2dt
```

安装完成功查看版本确定是否安装成功：

```
js2dt -v
```

## 使用

一般使用：

```
js2dt --src ./test.js
```

配置babel插件：

```
// babel.config.json

"plugins": [
    ["./node_modules/js2dt", {
      "overwrite": true
    }]
  ]
```

配合webpack使用：

```
// webpack.config.js

rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
            plugins: [
              ["./node_modules/js2dt", {
                "overwrite": true,
                "outdir": 'dist/type',
                "publicdir": ''
              }]
            ]
          }
        }
      }
    ]

```

命令配置项：

配置项|简写|作用|备注
---|---|---|---
--src [path]|-s|生成指定path路径的js文件的类型文件|-
--deep | \ | 对文件引用的文件也生成类型文件 | 会排除引用的库
--overwrite| -o | 生成时覆盖已存在的类型文件 | -

插件配置项：

配置项|类型|作用|备注
---|---|---|---
"overwrite"| boolean | 是否覆盖已存在的d.ts文件 | -
"outdir"| string | 输出d.ts文件的目录 | 不写则会默认在当前目录
"publicdir" | string| 源文件的公共路径 | 会去除原路径前的公共路径

## 注意项：

1. 必须指定文件路径。
2. d.ts文件会生成在js文件相同目录下
3. 对部分js语法解析可能会有问题。

## 更新计划

1. 对导出语句增加支持 √
2. 对相对路径导入语句增加支持 √
3. 增加类型推断能力，不完全依赖于jsdoc

## 无法解析的语法

1. ES5中类的写法
2. 声明类后为类添加属性

---
_Source: https://npm.io/package/js2dt · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
