简体   繁体   English

TypeScript -> JavaScript for browser:exports/require 语句

[英]TypeScript -> JavaScript for browser: exports/require statements

I want to switch from JavaScript to TypeScript for our web app's scripts.我想为我们的 Web 应用程序的脚本从 JavaScript 切换到 TypeScript。 However, when generating the JavaScript it always puts the following lines on top of the script:但是,在生成 JavaScript 时,它总是将以下几行放在脚本的顶部:

"use strict";
exports.__esModule = true;
var $ = require("jquery");

I receive browser errors for this.我收到浏览器错误。 How to prevent TypeScript from doing so?如何防止 TypeScript 这样做?

I read TypeScript: Avoid require statements in compiled JavaScript but it can't be the answer to switch to "any", this forfeits the whole TypeScript idea.我读过TypeScript:避免在编译的 JavaScript 中使用 require 语句,但它不能成为切换到“any”的答案,这放弃了整个 TypeScript 的想法。

I also read Typescript importing exported class emits require(...) which produces browser errors but this still generates the <reference... stuff into the JS file, which is also not what I want.我还阅读了Typescript importing export class 发出 require(...) 这会产生浏览器错误,但这仍然会在 JS 文件中生成<reference...东西,这也不是我想要的。

How to create "clean" JS files?如何创建“干净”的 JS 文件?

My tsconfig.json looks like this:我的 tsconfig.json 看起来像这样:

{
  "compilerOptions": {
    "noImplicitAny": true,
    "noEmitOnError": true,
    "sourceMap": true,
    "target": "es5"
  },
  "compileOnSave": true
}

My gulp call is:我的吞咽电话是:

var ts = require('gulp-typescript');
...
var tsProject = ts.createProject("tsconfig.json");
...
gulp.task("ts", function () {
  return gulp.src(tsInputFiles)
    .pipe(tsProject())
    .js
    .pipe(gulp.dest("wwwroot/js"));
});

Just drop the module loading in the beginning of your typescript file, if you do not want to load any modules?如果您不想加载任何模块,只需在打字稿文件的开头删除模块加载?

file1.ts:文件1.ts:

$(function () {
});

If you try to compile this, you'll get an error:如果你尝试编译它,你会得到一个错误:

file1.ts(2,1): error TS2581: Cannot find name '$'. Do you need to
install type definitions for jQuery? Try `npm i @types/jquery` and
then add `jquery` to the types field in your tsconfig.

Run npm as told above ( npm init first, if npm has not been initialized in the directory).按照上面的说明运行 npm(如果 npm 尚未在目录中初始化,请先npm init )。

Then add typeRoots and types to tsconfig:然后将typeRootstypes添加到 tsconfig:

{
  "compilerOptions": {
    "noImplicitAny": true,
    "noEmitOnError": true,
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [ "node_modules/types" ],
    "types": [ "jquery" ]
  },
  "compileOnSave": true
 }

After that, the compiling works, and you still have the strong typing in place (jquery types applied in compilation).之后,编译工作,并且您仍然拥有强类型(编译中应用的 jquery 类型)。

Browsers don't support JavaScript modules, so you'll need to tell the TypeScript compiler that you're trying to compile code that will be run on a browser.浏览器不支持 JavaScript 模块,因此您需要告诉 TypeScript 编译器您正在尝试编译将在浏览器上运行的代码。 I don't know what the rest of your project looks like, but in your configuration, try adding "module": "es2015" or "module": "system" to your configuration, while also adding an outFile defining where you want to file to be generated :我不知道您项目的其余部分是什么样子,但是在您的配置中,尝试将"module": "es2015""module": "system"到您的配置中,同时添加一个outFile定义您想要的位置要生成的文件:

in your tsconfig file :在您的 tsconfig 文件中:

{
  "compilerOptions": {
    "noImplicitAny": true,
    "noEmitOnError": true,
    "sourceMap": true,
    "target": "es5",
    "module": "es2015",
    "outFile": "script.js",
  },
  "compileOnSave": true
}

I wanted to use TypeScript files injected directly into a HTML file via gulp task (compiled into es5).我想用TypeScript文件直接注射到经过HTML文件gulp任务(编译成ES5)。 At first adding type="module" to <script> helped.首先将type="module"添加到<script>帮助。 But then more problems appeared, so finally I've end up using this notation instead of import :但是后来出现了更多问题,所以最后我最终使用了这个表示法而不是import

/// <reference types="@types/jquery" />
/// <reference path="my_custom_file.ts" />
/// <reference path="my_externalTypes.d.ts" />

and namespace in each of the file to separate them from each other.和每个文件中的namespace以将它们彼此分开。

That should work for a simple, small projects.这应该适用于简单的小型项目。

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

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