如何禁用typescript中的隐式导入

问题描述 投票:2回答:2

首先,“隐含进口”不是官方术语。

这就是我所谓的“隐式导入”的东西。

我有2个ts文件:

file1:app.ts

import * as angular from 'angular'; // (*)
import component from './component';

file2:component.ts

const component : angular.IComponentOptions = {
    templateUrl: 'component.template.html'
};

export default component;

在component.ts中,即使未导入angular,typescript编译器也不会显示任何错误。如果我在app.ts文件中删除(*)行,那么typescript编译器会抱怨没有角度命名空间,所以我认为当app.ts导入component.ts时,app.ts中导入的模块也会隐式导入component.ts中。这就是为什么我将此行为称为“隐式导入”。(如果存在,请告诉我此行为的官方用语)

如何禁用此行为?我想让每个ts文件尽可能完整。

typescript import module
2个回答
2
投票

首先,“隐含进口”不是官方术语。

您正在寻找的术语是global module。无论您使用哪种类型定义的angular定义了全局angular,因此您可以使用此全局而无需导入它。

如果类型定义定义了一个全局...并且您使用该类型定义(我假设您必须这样做)......您将获得全局。没有两种方法可以抱歉。


0
投票

你遇到过this TypeScript issue:像angular这样的UMD全局变量甚至可以在模块中用于类型位置。 Ryan Cavanaugh声称TypeScript按预期工作,但我同意该问题的记者的说法,这种行为毫无意义。

© www.soinside.com 2019 - 2024. All rights reserved.