简体   繁体   English

如何忽略打字稿中的导入语句

[英]How to ignore import statement in Typescript

log.ts has the following code log.ts 有以下代码

import {LOG} from './log'

LOG.e("tag","error");
LOG.f("tag","error");
LOG.d("tag","error");

I want the IntelliSense support for the TS file but don't want the consequences of import on generated javascript.我想要 TS 文件的 IntelliSense 支持,但不希望导入对生成的 javascript 的影响。

I simply want the following in my Javascript file (log.js)我只想在我的 Javascript 文件 (log.js) 中包含以下内容

    LOG.e("tag","error");
    LOG.f("tag","error");
    LOG.d("tag","error");

log.ts file must contain only declarations. log.ts文件必须只包含声明。

declare class LOG {
  public static e(a: string, b: string): void;
  public static f(a: string, b: string): void;
  public static d(a: string, b: string): void;
}

If you want to use an imported class, and it should not be included in the generated javascript, then you need to declare it using declare .如果你想使用一个导入的类,并且它不应该包含在生成的 javascript 中,那么你需要使用declare来声明它。

This class is not functional javascript code, but only declares the type of the class and its methods.这个类不是功能性的javascript代码,只是声明了类的类型及其方法。 When generating javascript, this class will not be included in the build.生成 javascript 时,该类将不会包含在构建中。

But to execute such javascript, LOG must already be declared in the global environment.但是要执行这样的javascript,必须已经在全局环境中声明了LOG

See playground ...游乐场...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM