Tag: 打字稿

将parameter passing给TypeScript编译器Node模块

我正在使用TypeScript节点NPM模块来编译我的项目中的.ts文件。 简单的情况 至于文档(这是在NPM页面浓缩在上面链接我导致),编译一个简单的文件时,我只需要: node node_modules/typescript/bin/tsc.js main.ts 不是那么简单的情况 但是,我需要传递参数给编译器,所以我这样做: node node_modules/typescript/bin/tsc.js main.ts –module commonjs –out out/main.js 但是它看起来像–module commonjs –out out/main.js部分不被考虑和丢失。 如何成功地将parameter passing给通过“节点”调用的tsc.js ? 谢谢

在Node.js的TypeScript 1.6.2中导入jQuery

我正在使用Node.js工作在一个爬虫。 我使用jQueryparsing使用jsdom构build的页面。 我通过tsd发现了一个jquery.d.ts ,它的结尾是这样的: declare module "jquery" { export = $; } declare var jQuery: JQueryStatic; declare var $: JQueryStatic; 这个定义似乎只适用于jQuery在全局加载或全局窗口variables可用的客户端… 正如这里所解释的,当在window.document不可用的环境(如Node.js)中导入(使用require )时,jQuery会导出一个必须使用窗口对象初始化的工厂: // JavaScript (ES5) var jquery = require("jquery"); // … var $ = jquery(window); 但是使用TypeScript,因为定义不包含这个工厂。 它不工作: // TypeScript import jquery from "jquery"; // Module '"jquery"' has no default export import {jquery} from "jquery" […]

如何编写打字稿方法来复制javascript方法

我在JavaScript中有这样的代码: // find the user User.findOne({ name: req.body.name }, function(err, user) { if (err) throw err; if (!user) { res.json({ success: false, message: 'Authentication failed. User not found.' }); } else if (user) { //issue token } }) User.findOne来mongoose。 但是我想嘲笑这个方法,所以我现在不会和数据库进行交stream。 但要保持这样的逻辑,所以我需要写我自己的User.findOne打字稿模拟,所以是相同的。 这就是我到目前为止: export class User { constructor() { } public findOne(user: any, next:Function) { console.log(1); […]

无法使用NPM安装TypeScript

我无法使用npm完成Typescript安装。 正如文档build议我运行以下命令: npm install -g typescript 要么 sudo npm install -g typescript – 这似乎是好的,直到大约20%的过程中,它不能进行,这是在shell中注销的错误。 npm ERR! fetch failed https://registry.npmjs.org/typescript/-/typescript-1.8.5.tgz npm WARN retry will retry, error on last attempt: Error: fetch failed with status code 504 npm ERR! fetch failed https://registry.npmjs.org/typescript/-/typescript-1.8.5.tgz npm WARN retry will retry, error on last attempt: Error: socket hang up npm ERR! […]

在TypeScript中声明JavaScript扩展函数

我有用JavaScript编写的这种types的Node.js模块: function main(options) { return "some string"; } main.methodName = function () { // implementation; }; main.objectName = { // a namespace; }; main.propertyName = 123; module.exports = main; 在TypeScript中声明这样一个接口的正确方法是什么? 澄清 我正在问如何在TypeScript中为现有的Node.js模块正确声明这样一个接口,所以它可以在TypeScript文件中正确使用,而不是如何在TypeScript中重新实现这样的接口。 UPDATE 根据@toskv的build议,我添加了以下界面: declare module "my-module" { // Default library interface interface main { (options?:{}):string, methodName():any, propertyName:any, objectName:Object } export default main; } 但是如果我这样使用它: […]

节点无法运行编译TypeScript

我有以下TypeScript var foo = (…myParams) => { for (var i = 0; i < myParams.length; i++){ console.log(myParams[i] + " "); } }; foo('a', 'b', 'c'); 但是,当编译的WebStorm在编译代码中发现错误时: 而当它由节点符文时,节点不会识别其余的expression式。 C:\PluralSight\TypeScript>node rest-parameter.js C:\PluralSight\TypeScript\rest-parameter.js:5 var foo = (…myParams) => { ^^^ SyntaxError: Unexpected token … at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:414:25) at Object.Module._extensions..js (module.js:442:10) at Module.load (module.js:356:32) at Function.Module._load […]

如何使用打字稿与nodeJS – “需要”的问题

我正在build立一个新的节点/打字稿项目,并且无法正常工作。 'use strict'; let fetch = require("node-fetch"); 给我error TS2304: Cannot find name 'require'. 所以我觉得打字是需要的? 尝试: typings install node typings install nodejs typings install require 所有这些给 Unable to find "XXX" ("npm") in the registry 什么是获取需求的正确方法,或者任何使用typescript的简单节点应用程序。 一旦你走了,TS是伟大的,但在初始configuration周围移动的沙子是一个相当麻烦去。 我也看了这个答案 ,但是并没有解决这个问题。 有些build议似乎不再有效。 我试图编译到ES6。 types版本1.0.5 使用tsconfig.json和typings.json我已经从一个单独的工作项目复制,并尽可能删除。 我想我必须用镊子来弄清楚什么是不同的。

无法在同一个TypeScript项目中使用requireJS和Node的Require

我有一个针对Node和浏览器的打字稿项目。 我在某些脚本中使用了Node的require() ,并且在其他脚本中require() 。 我的项目目录如下所示: myProject \– a.ts \– b.ts \– node.d.ts \– require.d.ts 其中a.ts包含: /// <reference path="./node.d.ts" /> var cp = require('child-process'); var a = 'hello world' export = a 和b.ts包含: /// <reference path="./require.d.ts" /> require('hello',function(x){console.log('world')}); var b = 'hello world' export = b 其中require.d.ts和node.d.ts从DefinitlyTyped获得。 当我编译我的项目,我得到这些错误: b.ts(2,1): error TS2346: Supplied parameters do not match […]

在angularfire2安装和设置过程中出现错误

我遵循Angularfire2安装和设置 (08-21-2016),但是当我键入时,在terminal中收到错误: ng build 这些是错误: Build error The Broccoli Plugin: [BroccoliTypeScriptCompiler] failed with: SyntaxError: Unexpected token ] at Object.parse (native) at BroccoliTypeScriptCompiler._loadTsConfig (C:\Users\Marian\Desktop\ng_cli\test_cli_fire\node_modules\angular-cli\lib\broccoli\broccoli-typescript.js:128:25) at BroccoliTypeScriptCompiler._createServiceHost (C:\Users\Marian\Desktop\ng_cli\test_cli_fire\node_modules\angular-cli\lib\broccoli\broccoli-typescript.js:163:25) at BroccoliTypeScriptCompiler.build (C:\Users\Marian\Desktop\ng_cli\test_cli_fire\node_modules\angular-cli\lib\broccoli\broccoli-typescript.js:41:12) at C:\Users\Marian\Desktop\ng_cli\test_cli_fire\node_modules\angular-cli\node_modules\broccoli-caching-writer\index.js:152:21 at lib$rsvp$$internal$$tryCatch (C:\Users\Marian\Desktop\ng_cli\test_cli_fire\node_modules\angular-cli\node_modules\rsvp\dist\rsvp.js:1036:16) at lib$rsvp$$internal$$invokeCallback (C:\Users\Marian\Desktop\ng_cli\test_cli_fire\node_modules\angular-cli\node_modules\rsvp\dist\rsvp.js:1048:17) at lib$rsvp$$internal$$publish (C:\Users\Marian\Desktop\ng_cli\test_cli_fire\node_modules\angular-cli\node_modules\rsvp\dist\rsvp.js:1019:11) at lib$rsvp$asap$$flush (C:\Users\Marian\Desktop\ng_cli\test_cli_fire\node_modules\angular-cli\node_modules\rsvp\dist\rsvp.js:1198:9) at nextTickCallbackWith0Args (node.js:420:9) at process._tickCallback (node.js:349:13) The broccoli plugin was instantiated at: […]

angular2-rc1 http.post不能正常工作

我正在创build一个angular2应用程序,我在我的服务中使用http进行POST调用到mongoDB 。 当我第一次进行POST调用时,它工作正常,即条目插入到数据库正确,但是当我第二次发送数据时,它不被插入。 如果我第一次插入后重新加载页面,那么它的工作正常。 我做了一些debugging,发现在第二次请求时,我的req.body是空白的。 这是我的代码: page.service.ts savePage(page: Object) { this.headers.append('Content-Type', 'application/json'); let url = this.baseUrl+'/pm/pages/'; let data={}; data["data"]=page; console.log(data); //this is printing both times correctly //on second request data is blank where as it works correctly for first time return this.http.post(url, JSON.stringify(data),{headers: this.headers}) .map((res: Response) => res.json()).catch(this.handleError); } 这是我在节点服务中显示的req.body中的数据。 首要要求 : body:{ data:{ name: […]