我为PlayCanvas游戏引擎编写了TypeScript声明文件。源代码在pc
命名空间下具有全局结构。我在编写声明文件时复制了源代码的文件和文件夹结构。我有50多个单独的d.ts文件,它们都使用declare namespace pc {}
。我现在想将这些声明文件贡献给DefinitelyTyped但是看看文档似乎他们可能只需要一个名为index.d.ts
文件的大文件中的所有内容。如何转换我的所有文件和文件夹,以便整个东西与DefinitelyTyped兼容?我真的必须把所有东西塞进一个文件中吗?我不能保留与引擎源代码相匹配的漂亮文件和文件夹结构吗?
答案 0 :(得分:1)
这是为大型库创建类型定义的常见难点。此时的最佳解决方案是使用实用程序(如dts-generator)生成index.d.ts以进行分发/使用(并将您的单个文件/文件夹保留为源)。
dts-generator半盲地将所有内容转储到一个文件中。在我的情况下(也恰好是游戏引擎,你可以看到here),该文件需要一些后处理才能与我重新导出库的不同部分的index.ts文件兼容。这是一个命令行实用程序,可以使用ts-node
(npm i ts-node -g
)运行。
import * as fs from 'fs';
import * as readline from 'readline';
const moduleDeclaration = 'module-typings.d.ts'; // file created by dts-generator
const indexDeclartion = 'index.d.ts';
const distPath = './dist/';
const indexOut: string[] = [];
const reader = readline.createInterface({
input: fs.createReadStream(`${distPath}${moduleDeclaration}`),
});
const moduleDeclaration = /^declare module/;
const malletImport = /(import|export).+from 'mallet/;
function isExcluded(line: string) {
return moduleDeclaration.exec(line) || malletImport.exec(line) || line === '}';
}
const identifiers = [];
const importLine = /import {(.*)} (.*)/;
const importRequire = /import (.*) = (.*)/;
function stripDuplicateDeclarations(line) {
let importResult;
if ((importResult = importLine.exec(line))) { // tslint:disable-line:no-conditional-assignment
const imports = importResult[1].replace(/\s/g, '').split(',');
const newImports = imports.filter((variable) => identifiers.indexOf(variable) === -1);
Array.prototype.push.apply(identifiers, newImports);
const importString = newImports.join(', ');
return line.replace(importLine, `import {${importString}} $2`);
} else if ((importResult = importRequire.exec(line))) { // tslint:disable-line:no-conditional-assignment
const importName = importResult[1];
if (identifiers.indexOf(importName) === -1) {
identifiers.push(importName);
return line;
}
return ''; // return an empty line if the import exists
}
return line;
}
const constLine = /^\s{2}const .+:/;
function declareConst(line) {
if (constLine.exec(line) !== null) {
return (`declare${line}`).replace(/\s+/, ' ');
}
return line;
}
reader.on('line', (line) => {
if (!isExcluded(line)) {
const finalLine = [stripDuplicateDeclarations, declareConst]
.reduce((processedLine, processor) => processor(processedLine), line);
indexOut.push(finalLine);
}
});
reader.on('close', () => {
const indexContext = indexOut.join('\n');
fs.writeFileSync(`${distPath}${indexDeclartion}`, indexContext, {encoding: 'utf-8'});
});
此脚本删除重复声明和嵌套命名空间,以便使用者可以轻松导入库组件。作为免责声明,此脚本只是一个适用于我的库的示例,并不打算处理所有情况。使用TypeScript API进行这种处理可能有更正确(和复杂)的方法。