Tag: 打字稿

未定义的NodeJS'toUpperCase'(由typescript生成)

为什么这个工作,当我从nodejs发射: var a = function (name) { return "Hello, " + name.toUpperCase(); }; var result = a("bob"); console.log(result) 但是这个: var A = (function () { function A(msg) { this.msg = " "; this.msg = msg; } A.prototype.hello = function (name) { return "Hello, " + name.toUpperCase(); }; A.prototype.helloToBob = function (fn) { return fn.apply("Bob"); }; […]

visual studio没有findfirebase节点模块

我使用命令npm install firebase –save从我的项目文件夹的根目录保存,并且firebase文件夹已成功添加到我的node_modules文件夹中,并且packages.json也被更新。 我也使用打字稿,所以我有一个app.ts文件,我尝试import Firebase = require("firebase"); 但是它不起作用。 我得到红色的曲折和错误说cannot find external module "firebase" 。 我已经安装快递,并在app.ts中导入它,并且工作正常,没有错误。 我试图通过firebase来源,但它的缩小和不可能尝试在那里做任何发现,关于什么是出口等。 任何想法可能是错误的,我怎样才能解决它?

在打字稿中使用Express中的callback函数数组

我试图在打字稿中重新创build快速路由示例 我有… /// <reference path='Scripts/typings/express/express.d.ts' /> import express = require('express'); var app = express(); var cb0: express.RequestHandler = function (req, res, next) { console.log('CB0'); next(); } var cb1: express.RequestHandler = function (req, res, next) { console.log('CB1'); next(); } var cb2: express.RequestHandler = function (req, res) { res.send('Hello from C!'); } var arr: express.RequestHandler[] = [cb0, […]

节点与Chrome,实施instanceof,多个文件

我有这个奇怪的行为,希望你能帮助我。 =) 我有一个设置,我需要使用Mocha运行JavaScripttesting。 代码由TypeScript生成,testing通过commonjs和commonjs模块在浏览器和nodejs中运行。 问题是,我想使用instanceof ,但显然,行为会根据代码是在Node还是在Chrome运行而有所不同。 示例如下。 // file Actions.ts Class Action {..} Class ViewAction extends Action {..} Class MyViewAction extends ViewAction {..} // file App.ts var action = new MyViewAction(); // file App.ts or Elsewhere.ts foo(action) { var testA = action instanceof MyViewAction; var testB = (<any>action.constructor).name === "MyViewAction"; } 问题是,如果在不同的commonjs模块中调用foo,则如果在Node运行,则testA将变为false ,但如果在Chrome运行则为true ,而testB在两种情况下都为true 。 […]

TypeScript外部节点模块有时会传送到module.exports和exports

我正在将我的节点应用程序转换为使用TypeScript外部模块的过程。 运行应用程序时一切正常,但转换我的一些.ts文件,由于SyntaxError: Unexpected reserved word摩卡testing“爆炸” SyntaxError: Unexpected reserved word 。 经过多次debugging,我发现以下可重现的故障情况。 我有一个简单的autoRoles.ts文件,它定义了可用的用户angular色。 在使用外部模块之前,它看起来像: /// <reference path="../../typings/backend_typings.d.ts" /> module.exports.roles = { // role definitions } 现在转换之后: /// <reference path="../../typings/backend_typings.d.ts" /> export let roles = { // role definitions } 运行摩卡testing时,会产生以下错误: >> Mocha exploded! >> SyntaxError: Unexpected reserved word >> at exports.runInThisContext (vm.js:53:16) >> at Module._compile (module.js:413:25) […]

TypeScript并没有在VS 2015中将JavaScript与Node.js TS应用程序结合到单个文件中

如果我做错了,请纠正我,或者误解这里的任何概念。 据我所知,通过这个连接错误报告的固定状态( https://connect.microsoft.com/VisualStudio/feedback/details/1581065/typescript-compile-output-into-single-file-not-保存工作 ),您应该能够通过使用TypeScript项目的项目属性中的“将JavaScript输出合并到文件”选项将TypeScript输出编译为单个文件。 通过文件 – >新build项目 – > TypeScript – >“空Node.js Web应用程序”,Visual Studio 2015更新1社区版中一个全新的项目可以重现此问题,我运行TypeScript 1.7.6这是最新的写作,而不是这样做。 它仍然将.js文件并行输出到.ts文件。 我也尝试在该字段中input整个文件path,也尝试了一个完整的文件path到“redirectJavaScript输出到目录”选项也无济于事。 我希望能够做到这一点的原因,是因为我通过Grunt任务亚军开始我的Visual Studio外的TypeScript编译器。

Typescript:属性“已解码”在types请求中不存在

我在JavaScript中有这样的代码: if (token) { // verifies secret and checks exp jwt.verify(token, Config.Secret, function(err, decoded) { if (err) { return res.json({ success: false, message: 'Failed to authenticate token.' }); } else { // if everything is good, save to request for use in other routes req.decoded = decoded; next(); } }); } else { // if […]

在TypeScript中使用外部模块声明全局variables

我在TypeScript https://www.npmjs.com/package/html2commonmark中创build了一个npm模块。 该模块可用于nodejs(通过使用require )和浏览器(通过在浏览器中加载node_modules/html2commonmark/dist/client/bundle.js )。 我最近添加了* .d.ts文件,以便在使用"moduleResolution": "node"时input信息"moduleResolution": "node" 。 这很好,当安装我的模块,它已准备好在打字稿中使用。 因此:下面的一段打字稿代码正在编译没有错误: // After installing using npm install html2commonmark // using "moduleResolution": "node" import * as html2commonmark from 'html2commonmark'; let converter = new html2commonmark.JSDomConverter(); 美丽! 现在我想在浏览器中运行我的模块。 如前所述,我需要添加一个脚本标记到node_modules/html2commonmark/dist/client/bundle.js到我的index.html页面。 之后,html2commonmark全局variables应该可用。 问题是:我如何让打字稿编译知道全局variables在那里? 以下的TS代码将不会编译: let converter = new html2commonmark.BrowserConverter(); // error TS2304: Cannot find name 'html2commonmark'. 即使我添加一个global.d.ts文件,我似乎无法同时导入我的外部模块,并声明我的全局variables: // Something […]

使用mongoose的打字稿错误

我用typescript@1.7.5,express@4.13.3,mongoose@4.3.7和mongodb@3.2.0使用node.js 我得到编译错误: TS2345:types'(error:any,document:any)=> void'的参数不能分配给types为'(err:any)=> void'的参数。 在这行: (error, document) => { 即使有这个编译错误,一切运行正常。 我怎样才能解决这个错误? import express = require("express"); import bodyParser = require('body-parser'); import mongoose = require("mongoose"); import contactListModel = require("./contactlistSchema"); var contact = contactListModel.contact; export function removeOne (req: express.Request, res: express.Response) { var id = req.params.id; console.log("delete one contact in database with id: " + id); contact.remove( […]

设置开发环境(node_modules重复)

(我是一个Actionscript开发者,最终迁移到JavaScript并且正在学习TypeScript和AngularJS 2让我知道这个问题太愚蠢/天真了,我会删除它) 我正在运行Angular 2教程 ,一切都很好,但是我想知道如何设置一个开发环境而不重复,例如“node_modules”和“typings”目录。 我明白,教程要自己完成,所以有重复。 我也得到,例如,下面的行是使用相对于项目文件夹的引用,所以它可以指向其他地方。 是否有避免重复节点模块的“最佳做法”? 我问,因为“node_modules”目录是145 MB。 import {Component} from 'angular2/core';