@mcswift/tsc
v1.0.19
Published
[English](#english) | [中文](#中文)
Readme
@mcswift/tsc
中文
提供基于 TypeScript 的声明文件生成工具,方便在打包构建等流程中快速生成 .d.ts 声明文件。
安装
npm install @mcswift/tsc
# or
pnpm add @mcswift/tsc注意: 使用该包需要在运行环境中已经安装了
typescript并确保在node_modules/.bin下能够找到tsc命令。
使用指南及接口文档
你可以使用 generatorDeclare 快速为指定目录生成 TypeScript 声明文件,底层会自动查找并调用 tsc。
1. 生成类型声明文件
默认会读取当前工作目录下的 tsconfig.json 并只输出声明文件(--emitDeclarationOnly)。
import { generatorDeclare } from "@mcswift/tsc";
async function buildDeclarations() {
// 指定源码输入目录和声明文件输出目录
await generatorDeclare("src", "types");
console.log("声明文件生成完毕!");
}
buildDeclarations();2. 自定义执行目录与配置文件
如果你的项目结构比较复杂,你可以指定执行的根目录和配置文件的路径:
import { generatorDeclare } from "@mcswift/tsc";
async function build() {
const rootDir = process.cwd(); // 或者指定特定的包目录
const tsconfigPath = "./tsconfig.build.json";
await generatorDeclare("src", "dist/types", rootDir, tsconfigPath);
}类型注释:
const generatorDeclare: (
inputDir: string,
outDir: string,
root?: string,
tsconfig?: string
) => Promise<void>;English
Provides a TypeScript-based declaration file generation tool, making it easy to quickly generate .d.ts declaration files during bundling and build processes.
Installation
npm install @mcswift/tsc
# or
pnpm add @mcswift/tscNote: Using this package requires
typescriptto be installed in your runtime environment, and ensuring thetsccommand can be found undernode_modules/.bin.
Usage Guide & API Documentation
You can use generatorDeclare to quickly generate TypeScript declaration files for a specified directory. It automatically locates and invokes tsc under the hood.
1. Generate Type Declaration Files
By default, it reads the tsconfig.json in the current working directory and only emits declaration files (--emitDeclarationOnly).
import { generatorDeclare } from "@mcswift/tsc";
async function buildDeclarations() {
// Specify the source input directory and the declaration file output directory
await generatorDeclare("src", "types");
console.log("Declaration files generated successfully!");
}
buildDeclarations();2. Custom Execution Directory and Configuration File
If your project structure is complex, you can specify the root directory for execution and the path to the configuration file:
import { generatorDeclare } from "@mcswift/tsc";
async function build() {
const rootDir = process.cwd(); // Or specify a specific package directory
const tsconfigPath = "./tsconfig.build.json";
await generatorDeclare("src", "dist/types", rootDir, tsconfigPath);
}Type Annotations:
const generatorDeclare: (
inputDir: string,
outDir: string,
root?: string,
tsconfig?: string
) => Promise<void>;