3

我一直在尝试使用 Visual Studio Code (0.3.0) 编辑器使用 TypeScript (1.5 beta) 和 AngularJS (1.4) 创建一个 hello-world 示例。如下图所示,当代码引用 AngularJS 的 TypeScript 定义文件时,VS Code 会抛出很多错误。 在此处输入图像描述

在此处输入图像描述

不知道我在这里做错了什么。

** 编辑 ** 首先运行安装类型npm install -g typescript,然后tsd install [library-name] --save

考虑到 GJSmith3rd 的评论,构建项目会输出 tsc 的 --help 命令。见下文: 在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

4

1 回答 1

1

VSCode 中的打字稿适用于您的示例。

  1. 在 VSCode 中创建一个新文件夹

  2. 使用编译器选项创建一个简单的 tsconfig.json 文件

    {
        "compilerOptions": {
            "target": "ES3",
            "module": "amd",
            "sourceMap": false
        }
    }
    
  3. 在 app.ts 中创建示例代码

    export interface IPerson {
        firstName: string;
        lastName: string;
    }
    
    export class Person implements IPerson {
        constructor(public firstName: string, public lastName: string) {
            var module = angular.module("myApp", []);
        }
    }
    
  4. 重要提示:使用来自definitelyTyped的DefinitelyTyped tsd命令 。Angular 依赖于 jQuery。$tsd install angular jquery --save

  5. 添加tsd.d.ts文件引用到app.ts

    /// <reference path="typings/tsd.d.ts" />
    
  6. .settings/tasks.json使用shift++在应用程序目录中配置一个Task Runner,ctl然后b选择“Configure Task Runner”。删除内容"args:[Hello World],或创建新的类似任务"args:[],

  7. shift使用+ ctl+使用 Task Runner 编译b

在此处输入图像描述

这是我使用的未注释任务运行器"args": [],

// A task runner that calls the Typescipt compiler (tsc) and 
// Compiles a app.ts program
{
    "version": "0.1.0",

    // The command is tsc. Assumes that tsc has been installed using npm install -g typescript
    "command": "tsc",

    // The command is a shell script
    "isShellCommand": true,

    // Show the output window only if unrecognized errors occur. 
    "showOutput": "silent",

    // args is the app.ts program to compile.
    "args": [],

    // use the standard tsc problem matcher to find compile problems
    // in the output.
    "problemMatcher": "$tsc"
}

如果在 VSCode 中编译仍然存在问题,请尝试从项目目录中的命令行获取线索。

tsc --module amd  --out app.js app.ts

并如前所述检查您的版本:

02:00:23 ツ gjsmith3rd@DV7:~/Workspaces/Examples/TypeScript/MSDN/MyProject5 >tsc --version message TS6029: Version 1.5.3

考虑将 tsc 更新到最新版本,在此编辑时为 v1.5.3,带有sudo npm install tsc -g.

于 2015-06-21T05:51:06.063 回答