我使用了很多我自己和第三方的图书馆.我看到"typings"目录包含一些用于Jquery和WinRT ......但它们是如何创建的?
我是TypeScript的新手,现在我通过我的项目结构在几个地方都有.ts文件:
app/
|-scripts/
|-app.ts
|
|-classes/
| |-classA.ts
| |-classB.ts
|
|-controllers/
| |-controllerA.ts
| |-controllerB.ts
|
|-otherStuff/
|-otherstuffA.ts
Run Code Online (Sandbox Code Playgroud)
现在,当我的文件被编译时,它们被编译到.ts文件所在的同一目录:
app/
|-scripts/
|-app.ts
|-app.js
|
|-classes/
| |-classA.ts
| |-classB.ts
| |-classA.js
| |-classB.js
|
|-controllers/
| |-controllerA.ts
| |-controllerB.ts
| |-controllerA.js
| |-controllerB.js
|
|-otherStuff/
|-otherstuffA.ts
|-otherStuffA.js
Run Code Online (Sandbox Code Playgroud)
虽然我喜欢.js文件与.ts文件保持相同目录结构的方式,但我不想跟踪我的VCS中的.js文件,因此我想将所有JavaScript文件保存在单独的目录树(我可以添加到.gitignore),如下所示:
app/
|-scripts/
| |-app.ts
| |
| |-classes/
| | |-classA.ts
| | |-classB.ts
| |
| |-controllers/
| | |-controllerA.ts
| | |-controllerB.ts
| |
| |-otherStuff/
| |-otherstuffA.ts
| …Run Code Online (Sandbox Code Playgroud) 我的意思是写一个类型的参数number,但我错误拼写了类型,Number而是写作.
在我的IDE(JetBrains WebStorm)上,类型Number使用与原始类型相同的颜色编写number,而如果我写了一个类的名称(已知或未知),它使用不同的颜色,所以我猜它以某种方式识别它拼写错误的类型作为正确/几乎正确/排序正确的类型.
当我编译代码时,而不是抱怨例如编译器找不到名为的类Number,TSC写入此错误消息:
Illegal property access
Run Code Online (Sandbox Code Playgroud)
这是否意味着number并且Number两者共存为不同的类型?
如果这是真的,这些类之间的区别是什么?
如果不是这种情况,那么为什么它只是没有写出它为未知类显示的相同错误消息("当前范围中不存在名称'数字'")
这是代码:
class Test
{
private myArray:string[] = ["Jack", "Jill", "John", "Joe", "Jeff"];
// THIS WORKS
public getValue(index:number):string
{
return this.myArray[index];
}
// THIS DOESN'T WORK: ILLEGAL PROPERTY ACCESS
public getAnotherValue(index:Number):string
{
return this.myArray[index];
}
}
Run Code Online (Sandbox Code Playgroud) 我正在拍打打字稿.它在hello world阶段运行良好.我现在正在尝试使用npm模块:
index.ts =
import _ = require('lodash')
console.log(_.toUpper('Hello, world !'))
Run Code Online (Sandbox Code Playgroud)
这不起作用:
tsc index.ts - > Cannot find module 'lodash'. (2307)node-ts index.js - > Cannot find module 'lodash'. (2307)看看打字稿文档和谷歌没有帮助.其他S/O问题要么没有答案(此处和此处)或不相关.
元素:
npm i --save lodash并存在于我的文件系统中(已选中)typings i --save lodashimport * as _ from 'lodash'或const _ = require('lodash')不起作用"moduleResolution": "node"并且"module": "commonjs"如某些答案所示,仍然无法正常工作我们如何在打字稿中使用npm包?
我在MacBook Air上运行.我安装了MS Code作为IDE并安装了TypeScript.
我有一个简单的文件,只有这一行:
import fs = require('fs');
Run Code Online (Sandbox Code Playgroud)
我在括号内的'fs'下得到一个红色波浪形,错误信息是[ts] Cannot find module 'fs'.该文件的扩展名为.ts.我是JavaScript和TypeScript的新手,但我的印象fs是核心模块,所以怎么能找不到它?我该如何解决这个问题?
我尝试过的其他事情:
tsc.我在那里得到一个基本相同的错误:error TS2307: Cannot find module 'fs'. sudo npm install fs -g.这报告显然取得了成功,但并未解决问题.我探索了SE和网络,但看起来很接近的答案似乎都假设'fs'可用.
有一个简单的TS包用作CommonJS模块,没有导出.TS文件被编译为具有相同名称的JS文件并用作require('package/option-foo').
tsconfig.json:
{
"compilerOptions": {
"target": "es5"
}
}
Run Code Online (Sandbox Code Playgroud)
期权foo.ts:
declare const GlobalVar: any;
function baz() {}
if (GlobalVar.foo) GlobalVar.baz = baz;
Run Code Online (Sandbox Code Playgroud)
期权bar.ts:
declare const GlobalVar: any;
function baz() {}
if (GlobalVar.bar) GlobalVar.baz = baz;
Run Code Online (Sandbox Code Playgroud)
这里最重要的部分是option-foo与option-bar被从来没有一起使用.项目中还有其他免费的TS文件,但它们不会影响任何东西,只需要在一次tsc运行中转换为JS .
当tsc运行时,它抛出
无法重新声明块范围变量'GlobalVar'.
重复的功能实现.
无法重新声明块范围变量'GlobalVar'.
重复的功能实现.
for GlobalVar和bazin两个文件.
如何在不使构建过程或这两个TS文件的输出复杂化的情况下进行处理?
当我tsc在终端中运行时(无论在哪里)我都会返回:
$ npx tsc --version
This is not the tsc command you are looking for
To get access to the TypeScript compiler, tsc, from the command line either:
- Use npm install typescript to first add TypeScript to your project before using npx
- Use yarn to avoid accidentally running code from un-installed packages
Run Code Online (Sandbox Code Playgroud)
据我所知,我没有在全球范围内安装 TypeScript,所以我期望它找不到tsc.
刚安装了VSxx的Typescript扩展,并按照Visual Studio 2012安装TypeScript,然后使用教程来调用编译器:
> tsc greeter.ts
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试编译.ts文件时,我应该在哪里键入:tsc greeter.ts?在VS命令行和Windows控制台中尝试,总是得到tsc未被识别为command(Command "tsc" is not valid.)的消息.
我想安装typescript,所以我使用了以下命令:
npm install -g typescript
Run Code Online (Sandbox Code Playgroud)
和测试tsc --version,但它只显示'找不到'tsc命令'.我已经尝试了很多方法,如stackoverflow,github和其他网站所建议的那样.但它不起作用.我怎么知道打字稿的安装位置和位置.
我的操作系统是Unix,OS X El Capitan 10.11.6,节点版本是4.4.3,npm版本是3.10.5
TypeScript专为大型JavaScripty项目而设计,通常包含多个内部生成的文件以及外部生成的库.TypeScript编译器(tsc)如何期望您为组成项目的完整文件集提供它?