Gri*_*ush 4 javascript module typescript ecmascript-6
我想迭代所有模块导出以提取它们的类型。
我假设在编译期间这些信息是可用的
export const a = 123;
export const b = 321;
// Is there any way to do something like this in typescript/javascript es6?
console.log(module.exports)
// { a: 123, b: 321 }
Run Code Online (Sandbox Code Playgroud)
编辑:我想分享我在第一行提到的问题的解决方案。
感谢您的回答,但看来我将其完全限制为单个文件是错误的。我创建了一个单独的types.ts并使用“实用程序类型”库来进行相关类型操作
import { ValuesType } from 'utility-types';
import * as myModue from './myModule';
export IMyModuleType = ValuesType<typeof myModue>;
Run Code Online (Sandbox Code Playgroud)
然后使用新的 3.8 语法导入类型
import type { IMyModuleType } from './types';
//...
Run Code Online (Sandbox Code Playgroud)
您可以使用 typescript Compiler API。它为您提供所需的所有信息
import * as ts from "typescript";
function compile(fileNames: string[], options: ts.CompilerOptions): void {
let program = ts.createProgram(fileNames, options);
let emitResult = program.emit();
console.log(program)
}
compile(['your-filename.ts'], {
noEmitOnError: true,
noImplicitAny: true,
target: ts.ScriptTarget.ES5,
module: ts.ModuleKind.CommonJS
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1718 次 |
| 最近记录: |