只需遵循最简单的Typescript教程,当涉及到键入函数参数时,我就失败了。
function greeter(person: string) { return "Hello " + person; } var user = "Jack"; document.body.innerHTML = greeter(user);但是,当我加载页面时,我得到“意外的令牌:” - 浏览器正在尝试解析Javascript? 我知道Tyescript会编译成JS,但我在这里遗漏了一些东西。
我正在使用Visual Studio 2013,使用jQuery.d.ts来键入它。
.ts正在通过TypeScriptCompile进行构建操作。
.aspx的正文如下所示:
<div> <script src="Scripts/Main.ts"></script> </div>Just following the simplest Typescript tutorial and when it comes to typing a function parameter I'm failing.
function greeter(person: string) { return "Hello " + person; } var user = "Jack"; document.body.innerHTML = greeter(user);But when I load the page I'm getting "Unexpected token :" - the browser is trying to parse Javascript? I get that Tyescript will compile down to JS, but I'm missing something here.
I'm using Visual Studio 2013, with jQuery.d.ts to type it.
The .ts is going through TypeScriptCompile for its build action.
Body for the .aspx looks like this:
<div> <script src="Scripts/Main.ts"></script> </div>最满意答案
您试图直接包含.ts文件,浏览器不明白。 在这种情况下,它会遇到它遇到的第一个类型定义,即分号
function greeter(person: string) {
您需要做的是将Scripts/Main.ts文件编译为Scripts/Main.js文件(VS2013应自动为您执行此操作),然后将其包含在.aspx源代码中。
要验证这一点,解决方案资源管理器中的Main.ts文件旁边应该有一个+号, Main.js文件应该在其下面。
You are trying to include the .ts file directly, and the browser does not understand that. In this case, it balks on the first type definition it encounters, i.e. the semicolon in
function greeter(person: string) {
What you need to do is compile the Scripts/Main.ts file to a Scripts/Main.js file (VS2013 should do this automatically for you), and then include that in the .aspx source code.
To verify this, there should be a + sign next to the Main.ts file in the solution explorer, and the Main.js file should be under that.
更多推荐
发布评论