我对typescript定义文件的理解是它们用于提供工具支持,而不是编译typescript所必需的.
但是考虑到以下因素:
app.ts
import {Observable} from 'rx';
Observable
.interval(1000)
.subscribe(n => console.log(n));
Run Code Online (Sandbox Code Playgroud)
运行:
npm install typescript rx --save
.\node_modules\.bin\tsc app.ts --module commonjs
Run Code Online (Sandbox Code Playgroud)
给出错误:
app.ts(1,26): error TS2307: Cannot find module 'rx'.
Run Code Online (Sandbox Code Playgroud)
导入rx的类型定义修复此问题
app.ts
/// <reference path="./node_modules/rx/ts/rx.all.d.ts" />
import {Observable} from 'rx';
Observable
.interval(1000)
.subscribe(n => console.log(n));
Run Code Online (Sandbox Code Playgroud)
问题
basarat对于该typings物业是正确的.有关更多信息,请参阅npm包的类型
看来需要定义文件,总是这样吗?
不会。如果模块使用该typings属性正确执行它,它就会正常工作。更多: https: //basarat.gitbooks.io/typescript/content/docs/quick/nodejs.html
rx npm 包包含定义文件。Typescript 可以自动搜索节点模块文件夹来查找它们,而无需我显式引用它们吗?
除非他们typings设置正确……但他们没有。