Tag: 打字稿

为什么添加这需要导入停止Node.js输出到控制台

我刚刚开始一个新的节点项目,仍然不太了解如何使用外部模块。 我有app.ts: import SampleClass = require( "./sampleModule" ); console.log( "Hello Node" ); var checker: SampleClass = new SampleClass(); 和sampleModule.ts: class SampleClass { constructor() { console.log( "Hello from sample"); } } export = SampleClass; (我也试过以下,我认为是ES5模块语法) export class SampleClass { constructor() { console.log( "Hello from sample"); } } 和我的tsconfig: { "compilerOptions": { "target": "es5", "module": "commonjs", "out": […]

我怎样才能得到Visual Studio Typescript 1.7 intellisense与nodejs库(angular2,rxjs)?

[已解决]我logging了一个对TypeScript的错误 ,修复应该汇总到下一个VS插件版本。 本质上,如果您指定模块分辨率为“NodeJs”,则可以使智能感知工作。 但编译器不喜欢这个,因为它期望它是“节点”。 它看起来像他们正在做的修复是通过指定“节点”,使一切工作。 我正在尝试使用msbuild构build一个Angular2项目,因为我想使用ASP.Net Web API作为我的后端。 代码编译和运行良好,但我的编辑器(SublimeText 3或Visual Studio 2015)不喜欢我导入Angular2库的方式: boot.ts: import {bootstrap} from 'angular2/platform/browser'; // error: cannot find module import {AppComponent} from './app.component'; bootstrap(AppComponent, []); 如果我使用TypeScript编译器直接使用tsconfig.json和node lite-server作为文档运行相同的示例( Angular2 Hero教程 ),那么SublimeText3可以工作,并且可以很好地识别此语法。 但是,一旦我切换到msbuild,由于某种原因,我得到这个intellisense错误。 以下是我认为相关的其他项目: 文件夹结构: \ – index.html – package.json \app – boot.ts – app.component.ts \node_modules \angular2 \platform – browser.js – browser.d.ts 的package.json: { […]

如何扩展Error类?

这是我的代码: let errorBadRequest = new Error("Bad request"); res.statusCode = 400; errorBadRequest.errors = err.details.reduce(function(prev, current) { prev[current.path] = current.message; return prev; }, {}); throw errorBadRequest; 我想在错误实例中扩展error属性,但是tsc表示joi-utils.ts(21,23): error TS2339: Property 'errors' does not exist on type 'Error'. errors的结构是{fieldname: fieldmsg} ,这是根据我的joi请求模式来决定的。 如何解决从打字稿编译器的错误? 我想我需要声明一个接口并指定属性。

编译时TypeScript找不到外部模块

我有一个项目,我基于angular2种子存储库( https://github.com/mgechev/angular2-seed ),我试图添加一个快递服务器后端写入TypeScript。 我的目录结构是相同的,除了我添加了一个名为server/到src/的文件夹。 我运行typings install ,我可以看到express.d.ts是typings/目录,但由于某种原因编译我的代码时,我总是得到以下错误(使用typescript@1.8.7 ): > npm start > angular2-seed@0.0.0 start C:\Users\Cody\projects\angular2-seed > tsc –outDir build/ –module commonjs ./src/server/server.ts && node ./build/server.js src/server/server.ts(1,26): error TS2307: Cannot find module 'express'. ./src/server/server.ts: import * as express from 'express'; let app = express(); app.get('/', function(req, res) { res.send('Hello World'); }); app.listen(3000, 'localhost'); console.log('Listening on port […]

如何在TypeScript中使用本地types打印纯JS文件

我有一个简单的JS模块(vendored),可以导出一个函数: 在./vendor/my-lib.js : export function doStuff(mandatoryArgumentX, optionalArgumentY) { } 我也有types – 在./vendor/my-lib-typings.d.ts 。 export function doStuff(mandatoryArgumentX: number, optionalArgumentY?: string) 现在,我有一个TypeScript模块,想要使用它: import {doStuff} from '../vendor/my-lib'; doStuff(44); 如何让我的TypeScript模块在处理此导入时使用键入? 全局模块(使用import 'my-lib' )很容易,因为我可以在types中放入相同的模块名称。 不过这里并不是这样,因为path确实是dynamic的。 有什么我可以把任何这三个文件,将解决这个难题(除了重写JS到TS)?

从Typescript类定义ExpressJS路由

我试图从一个Typescript类加载ExpressJS路线。 这个想法是,这应该最终是dynamic路线。 但是我已经被困在一个class上定义硬编码路线了。 我的index.ts js看起来像这样: import generic = require('./generic'); import Generic = generic.Generic; class Test { private app:any; private port:number = 3000; constructor() { this.app = express(); new Generic(); this.app.listen(this.port, () => { console.log('Listening on port ' + this.port); }); } } export = Test; new Test(); 然后我的generic.ts看起来像这样。 我试图从这个类定义另一条路线: module Generic { export class Generic […]

使打字稿节点模块一起工作

有人得到一个安装工作​​在哪个模块1使用typescript引用另一个模块2也打字,都使用tsdtypes,如node.d.ts? 我没有问题编译和使用它们,只要我只需要模块2.但只要我使用导入,而不是我重复的标识符地狱,由于这两个模块中的源文件导入例如node.d 。明显不同的path。 两个项目中的类都使用例如'streams'或'lodash',因此两者都使用types,因此使用///引用语法。 两个项目中的tsconfig都不包括types。

如何使承诺的打字稿工作?

所以,我在节点/ express / mongoose应用程序上使用打字稿,我试图让我的代码typecheck没有错误。 我定义这个mongoose模型: import * as mongoose from 'mongoose'; const City = new mongoose.Schema({ name: String }); interface ICity extends mongoose.Document { name: string } export default mongoose.model<ICity>('City', City); 和这个控制器: import * as Promise from 'bluebird'; import CityModel from '../models/city'; export type City = { name: string, id: string }; export function getCityById(id […]

在打字稿中,不能使promisifyAll Redis api与Bluebird

我无法使打字稿中的redis api promisifyAll。 import * as redis from 'redis'; import {RedisClient} from 'redis'; import * as Promise from 'bluebird'; Promise.promisifyAll(redis); const client: RedisClient = redis.createClient(CacheConfig); 我收到以下打字错误。

插入对象mongodb的私有variables

我正在使用以下环境 NodeJS:5.7.1 Mongo DB:3.2.3 MongoDB(NodeJS驱动程序):2.1.18 TypeScript:1.8 我已经使用Typescript创build了一个对象 class User { private _name:string; private _email:string; public get name():string{ return this._name; } public set name(val:string){ this._name = val; } public get email():string{ return this._email; } public set email(val:string){ this._email = val; } } 使用mongodb驱动程序API,我试图插入对象 var user:User = new User(); user.name = "Foo"; user.email = "foo@bar.com"; db.collection('users').insertOne(user) .then(function(r){..}).catch(function(e){..}); […]