visual-studio-code - Visual Studio Code 无法识别内置的 typescript 定义文件

标签 visual-studio-code typescript1.5

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

enter image description here

不确定我在这里做错了什么。

** 编辑 ** 通过首先运行 npm install -g typescript 然后 tsd install [library-name] --save

安装类型

考虑到 GJSmith3rd 的评论,构建项目输出 tsc 的 --help 命令。见下文: enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

最佳答案

VSCode 中的 Typescript 可以很好地处理您的示例。

  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 tsd 命令 $tsd install angular jquery --save 来自 DefinitelyTyped . Angular 依赖于 jQuery。

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

    /// <reference path="typings/tsd.d.ts" />
    
  6. 使用 shift+ctl+< 在应用程序目录的 .settings/tasks.json 中配置任务运行器kbd>b 并选择“配置任务运行器”。删除 "args:[Hello World], 的内容或使用 "args:[],

  7. 创建一个新的类似任务
  8. 使用shift+ctl+b

  9. 编译Task Runner

enter image description here

这是我使用的未注释的任务运行器 "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 消息 TS6029:版本 1.5.3

考虑使用 sudo npm install tsc -g 将 tsc 更新到最新版本,在进行此编辑时为 v1.5.3。

关于visual-studio-code - Visual Studio Code 无法识别内置的 typescript 定义文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30959148/

相关文章:

visual-studio-code - 如何在 Circle CI 上运行 VS Code 扩展测试?

Typescript baseUrl 不影响 VS Code 中的模块解析

android - Visual Studio 代码无法检测到模拟器设备或连接的电话

visual-studio-code - 如何导出 Visual Studio Code 扩展列表?

javascript - LINQ SingleOrDefault() 等效项

visual-studio-code - 当前目录的环境变量

javascript - AngularJS + TypeScript + ES6 模块 : how to bundle for browser?

javascript - Angular JS 2.0 将 typescript 编译为 javascript

windows - 为什么在 Mac 上编译 TypeScript 比在 PC 上快得多?

javascript - typescript 字符串 | string[] 求数组长度