Tag: 打字稿

在TypeScript中,从`import`和`import require`之间的区别

我使用node.js,最近我决定给TypeScript一个镜头,但我有点混淆模块如何导入。 我看到两种不同的语法,我无法确切地发现它们有什么不同: import * as a from 'a'; // ES6 standard to import stuff // OR … import a = require('a'); 这些都是一样的吗? 如果他们不是,我应该在哪里使用他们每一个?

Node.js中的相同命名空间中的每个文件的TypeScript类

什么是一个类每个文件多个类(因此多个文件)贡献给同一个命名空间的模式? 我在Node.js上下文中具体询问。 我知道如何在同一个命名空间中为每个文件定义一个类: 富/ A.ts: module foo { export class A { } } 富/ B.ts: module foo { export class B { } } main.ts: /// <reference path="./foo/A.ts"/> /// <reference path="./foo/B.ts"/> // TODO: How do I import the foo namespace at runtime? TypeScript被认为是应用程序级JavaScript开发的一种语言,但我们似乎在应用程序级开发的最基本的方面被冷落,那就是如何布局和构造代码文件以及如何在运行时将所有东西链接在一起。

试图编译“zone.js”作为外部模块,但它看起来像一个全局模块

我有“AngularClass”angular2-webpack-starter项目我已经安装了所有的npm依赖现在我试图安装types typings.json { "dependencies": { "zone.js": "github:gdi2290/typed-zone.js#66ea8a3451542bb7798369306840e46be1d6ec89" }, "devDependencies": {}, "ambientDependencies": { "angular-protractor": "github:DefinitelyTyped/DefinitelyTyped/angular-protractor/angular-protractor.d.ts#64b25f63f0ec821040a5d3e049a976865062ed9d", "core-js": "registry:dt/core-js#0.0.0+20160317120654", "hammerjs": "github:DefinitelyTyped/DefinitelyTyped/hammerjs/hammerjs.d.ts#74a4dfc1bc2dfadec47b8aae953b28546cb9c6b7", "jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#4b36b94d5910aa8a4d20bdcd5bd1f9ae6ad18d3c", "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#8cf8164641be73e8f1e652c2a5b967c7210b6729", "selenium-webdriver": "github:DefinitelyTyped/DefinitelyTyped/selenium-webdriver/selenium-webdriver.d.ts#a83677ed13add14c2ab06c7325d182d0ba2784ea", "webpack": "github:DefinitelyTyped/DefinitelyTyped/webpack/webpack.d.ts#95c02169ba8fa58ac1092422efbd2e3174a206f4" } } 当我键入 sudo typings install 我有 derzunov:angular2-webpack-starter derzunov$ sudo typings install typings ERR! message Attempted to compile "zone.js" as an external module, but it looks like a global […]

节点subprocess执行命令失败,错误代码为1

我正在尝试执行一些使用节点jssubprocess和获取错误的行。 以下是我的代码: let cmd : string = "code " + PROJECTS[value]; exec(cmd, function callback(error, stdout, stderr) { console.log("started console app"); }); 错误: cmd:"C:\WINDOWS\system32\cmd.exe /s /c "code c:\Users\shana\Dropbox\code-settings-syn… (length: 82)" code:1 killed:false message:"Command failed: C:\WINDOWS\system32\cmd.exe /s /c "code c:\Users\shana\Dropbox\c… (length: 99)" signal:null stack:undefined 错误JSON的细节。 Full CMD : "C:\WINDOWS\system32\cmd.exe /s /c "code c:\Users\shana\Dropbox\code-settings-sync"" Full message : "Command […]

Express和Typescript – Error.stack和Error.status属性不存在

我试图从JavaScript转换现有的node.js项目打字稿。 我一直在使用Visual Studio Express 4模板的默认404错误捕获器: // catch 404 and forward to error handler app.use(function (req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); 但是,我收到以下错误消息: types'错误'上不存在属性“状态”。 如果我尝试并调用Error的.stack属性,则会收到类似的消息: 属性“堆栈”在types“错误”中不存在。 有人知道这里发生了什么? 编辑:史蒂夫芬顿指出,我可以把错误状态的响应对象。 但是,我的error handling机制使用了两个步骤: 创build404错误并设置其状态 将它交给以下通用处理程序: app.use(function (err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: {} }); […]

在node.js中扩展TypeScript Global对象

我有一个node.js应用程序将一些configuration信息附加到global对象: global.myConfig = { a: 1, b: 2 } TypeScript编译器不喜欢这样做,因为Globaltypes没有名为myConfig对象: TS2339:“全局”types中不存在属性“myConfig”。 我不想这样做: global['myConfig'] = { … } 我该如何扩展Globaltypes来包含myConfig或者告诉TypeScriptclosures并相信我? 我更喜欢第一个。 我不想更改node.d.ts的声明。 我看到这个SOpost,并试图这样做: declare module NodeJS { interface Global { myConfig: any } } 作为扩展现有Global接口的一种方式,但它似乎没有任何效果。

使用TypeScript 2.0导入js文件

抽象 我试图从外部位置(即node_modules )导入“.js”文件我试图做到这一点使用commonjs模块模式,但导入不希望与“.js”文件types的工作,直到我添加“ .d.ts“文件附近的”.js“文件放在同一个文件夹中。 但问题是我不想用我的“.d.ts”文件影响任何node_modules 。 我希望它位于另一个文件夹,从node_modules分开,但只要我这样做,打字稿编译器会引发错误: 例 我有以下文件夹结构: |- DTS | |- ydts |- main.ts |- y.js y.js有以下内容 module.export = function (x) { console.log(x); }; ydts有以下内容 export interface Y { (x): any; } declare let y: Y; export default y; main.ts具有以下内容 import * as y from './y' 现在,当我试图编译main.ts : tsc -m commonjs -t ES2015 […]

使用Typescript扩展快速请求对象

我试图添加一个属性来使用打字稿从中间件表示请求对象。 不过,我不知道如何添加额外的属性的对象。 如果可能,我宁愿不使用括号表示法。 我正在寻找一个解决scheme,让我写一些类似的东西(如果可能的话): app.use((req, res, next) => { req.property = setProperty(); next(); });

打字稿:“无法find模块”有效打字

我刚刚开始使用打字机新的nodejs项目。 我安装了Typings( https://github.com/typings/typings ),并使用它来为节点v4.x安装参考文件,并且表示v4.x. 我的节点版本是v4.2.6我的打字版本是v1.7.5 我的项目目录是这样布置的: package.json tsconfig.json typings.json src/ app.ts typings/ main.d.ts main/ambient/node/node.d.ts main/ambient/express/express.d.ts typings / main.d.ts的内容如下: /// <reference path="main/ambient/express/express.d.ts" /> /// <reference path="main/ambient/node/node.d.ts" /> tsconfig.json的内容如下: { "compilerOptions": { "target": "es6", "module": "commonjs" } } typings.json的内容如下: { "dependencies": {}, "devDependencies": {}, "ambientDependencies": { "express": "github:DefinitelyTyped/DefinitelyTyped/express/express.d.ts#dd4626a4e23ce8d6d175e0fe8244a99771c8c3f2", "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#1c56e368e17bb28ca57577250624ca5bd561aa81" } } src / app.ts的内容如下: 'use strict'; […]

在nodejs中inputTypeScript模块

使用打字机在nodejs中导入模块的最佳做法是什么? 我来自C#背景,所以我想要做这样的事情 MyClass.ts module MyNamespace { export class MyClass { } } app.ts // something like using MyNamespace new MyNamespace.MyClass(); 要么 MyClass.ts export class MyClass { } app.ts import MyClass = module("MyClass") new MyClass(); 我知道我可以做到这一点,它会起作用,但是我必须为每个class级考虑两个名字 import MyClass2 = module("MyClass") new MyClass2.MyClass(); Point将类分隔为多个.ts文件(最好每个类一个文件)。 所以问题是,这是如何完成的?