typescript函数参数类型意外令牌:(typescript function parameter type unexpected token :)

编程入门 行业动态 更新时间:2024-10-20 07:48:39
typescript函数参数类型意外令牌:(typescript function parameter type unexpected token :)

只需遵循最简单的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.

更多推荐

本文发布于:2023-07-05 06:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034406.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   函数   意外   参数   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!