我一直在尝试使用 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 命令。见下文:
最佳答案
VSCode 中的 Typescript 可以很好地处理您的示例。
在 VSCode 中创建一个新文件夹
使用编译器选项创建一个简单的 tsconfig.json 文件
{ "compilerOptions": { "target": "ES3", "module": "amd", "sourceMap": false } }
在 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", []); } }
重要:使用 DefinitelyTyped
tsd
命令$tsd install angular jquery --save
来自 DefinitelyTyped . Angular 依赖于 jQuery。添加一个
tsd.d.ts
文件引用到app.ts
/// <reference path="typings/tsd.d.ts" />
使用 shift+ctl+< 在应用程序目录的
.settings/tasks.json
中配置任务运行器kbd>b 并选择“配置任务运行器”。删除"args:[Hello World],
的内容或使用"args:[],
创建一个新的类似任务
使用shift+ctl+b
编译Task Runner
这是我使用的未注释的任务运行器 "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/