【问题标题】:Typescript error when casting铸造时打字稿错误
【发布时间】:2016-10-06 09:21:44
【问题描述】:

我正在运行一个运行 Jasmine 和 Karma 的种子项目。 一切都设置正确。

Webpack 启动应用程序“npm start” 但是当我运行“npm test”时,Webpack 会查看一个 .ts 文件,然后加载 .spec.ts 文件。 从种子项目运行此代码时出现错误

bundle is now VALID.
[32m06 10 2016 10:16:42.660:INFO [karma]: 
[39mKarma v0.13.22 server started at http://localhost:9876/

[32m06 10 2016 10:16:42.664:INFO [launcher]: 
[39mStarting browser PhantomJS
[32m06 10 2016 10:16:44.259:INFO [PhantomJS 2.1.1 (Windows 7 0.0.0)]: 
[39mConnected on socket /#YRouUOeCLSlLTu_KAAAA with id 23215732
PhantomJS 2.1.1 (Windows 7 0.0.0)

ERROR
  SyntaxError: Unexpected token '<'
  at C:/Development/ScratchApp/ScratchApp/src/test.ts:4

PhantomJS 2.1.1 (Windows 7 0.0.0)

错误是 Unexpected token '

// this file is only being used by karma
require('phantomjs-polyfill')

requireAll((<any>require).context("./", true, /spec.ts$/));
function requireAll(r: any): any {
    r.keys().forEach(r);
}

有人能看到这个语法错误吗?

【问题讨论】:

  • 似乎您正在尝试运行打字稿代码。您应该首先将 typescript 代码编译成 javascript 代码,然后运行它。
  • 是的,你是对的。由于源代码控制丢失了映射文件(和定义),问题比我在这里发布的要复杂得多。由于某种奇怪的原因,WebPack 在构建服务器上查看了错误的源

标签: typescript webpack karma-runner


【解决方案1】:

这个问题的重要部分是:

at C:/Development/ScratchApp/ScratchApp/src/test.ts:4

这就是为什么您会得到一个意外的标记“requireAll 开头的行上有 &lt;any&gt;。 JavaScript 不期望找到类型注解,这就是为什么它们在编译过程中都会被删除。

更新您的运行时代码以使用编译后的文件:

C:/Development/ScratchApp/ScratchApp/src/test.js

【讨论】:

    猜你喜欢
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 2017-06-26
    相关资源
    最近更新 更多