我正在使用 Typescript 构建一个大型库,其中包含 100 个独立的 ts 文件。
以前我用过导出模块XXX(重命名为导出命名空间 XXX稍后)对于我的所有课程,但正如书籍所说,这不是推荐的方法,我应该使用 import 代替。
所以我尝试导入。这工作得很好:
import * as mylib from "./source/source.ts";
但由于我有 100 个文件,我不想为所有文件添加这样一行。我希望我的所有课程都可以通过mylib多变的。
所以我尝试了这个:
import * as mylib from "./source/";
但一旦我这样做,我就会得到:找不到模块“./source/”
有没有办法用一行导入具有多个文件的文件夹中的所有类?
tsc 提供的两种模块解析策略都不支持这种行为。您想要什么进口声明
import * as mylib from "./source/";
实际上所做的是按以下顺序执行检查:
1. (does package.json have a typings key? If so, import this file)
2. import * as mylib from "./source/index.ts";
3. import * as mylib from "./source/index.tsx";
4. import * as mylib from "./source/index.d.ts";
我假设您在这里使用节点式模块解析,您可能会这样做,因为这是推荐的方式。检查打字稿文档 https://www.typescriptlang.org/docs/handbook/module-resolution.html有关如何在 TypeScript 中完成模块解析的更多详细信息。
通常,您想要实现的目标是创建一个index.d.ts
文件,它用作导出其余模块的入口点。
我以 angular2 为例:
您常见的 angular2 导入如下所示:
import { Injectable } from '@angular/core'
core
只是一个位于其中的目录@angular
目录。就像你的一样source
目录。然而,在核心目录中有一个index.d.ts
file:
/**
* @module
* @description
* Starting point to import all public core APIs.
*/
export * from './src/metadata';
export * from './src/util';
export * from './src/di';
....
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)