Tag: typescript2.0

npm更新不能更改版本

我最近通过https://nodejs.org安装了nodejs v6.9.2我尝试在cmd中更新软件包,但是看起来没有任何改变。 我可以用这些命令来更改npm版本,在我的电脑中nodejs有些问题。下面是输出: 我怎样才能解决它 编辑:这里是npm和节点版本输出。

在TypeScript中使用枚举,并在每个枚举上使用方法

我有这个TypeScript文件: export type TSumanToString = () => string; export interface ISumanEvent { explanation: string, toString: TSumanToString } export interface ISumanEvents{ [key: string]: ISumanEvent } export const events: ISumanEvents = Object.freeze({ // runner events TEST_FILE_CHILD_PROCESS_EXITED: { explanation: 'runner is started, fires before any test child processes are started.', toString: makeToString('TEST_FILE_CHILD_PROCESS_EXITED') }, RUNNER_EXIT_CODE: { explanation: 'runner is […]

TypeScript导入与ES6对象解构

随着TSimport,我相信我可以做到这一点: import {foo as bar} from 'foo'; 与JS或TypeScript的ES6对象解构 – 有没有办法以同样的方式重命名“导入的值”? 例如, const {longVarName as lvn} = x.bar;

代码的作品,但最好的做法是有一个try / catch块内的承诺?

用Promise包装try/catch是不是很好? 我正在审查下面的代码,我似乎无法理解在Promise中使用try/catch块的必要性。 另外,为什么没有Promise拒绝的地方,正如你可以在catch块中看到Promise解决的那样。 请帮我理解这段代码,就最佳实践和效率而言,在Promise中有try/catch的意思。 我也非常感谢任何有关清理代码的build议。 getRoute(): any { return async (request: any, reply: any) => { const result = new Promise<string>( async (resolve, reject) => { try { let userIsValid = await this.validator.validate(request.payload, RegisterUserValidator); if (userIsValid.error) { resolve(responseHelper.getErrorResponse(ResponseErrorCode.joiValidatorError, userIsValid.error)); throw new ControlFlowNonError(); } let results = await this.authUser.registerUser(request.payload); resolve(responseHelper.getSuccessResponse(results, null)); } catch (error) { let […]

使用TypeScript的variables参数 – 强制最后一个参数

假设我们有一个接受可变数量参数的函数,我相信它被称为可变参数函数。 像这样的东西: function foo(){ let args = Array.from(arguments).map(v => whatever); // do something with args } 此函数的TypeScript接口可能如下所示: interface IVariadicFn { (…args: string[]): void, } 但是让我们说我们期望可变数量的string,但最终的参数,我们希望是一个callback函数。 所以我们有这样的东西: function variadic(){ let args = Array.from(arguments) let fn = args.pop(); let $args = args.map(v => whatever); // do something with args process.nextTick(function(){ fn(null, $args); // fire the callback, asynchronously […]

dynamicTypeScript导入,不使用Promises

我有这种情况。 他们dynamic的原因是为了防止加载10倍的代码量,我需要加载执行某个命令行任务。 if (diagnostics) { require('./lib/cli-commands/run-diagnostics').run(sumanOpts); } else if (script) { require('./lib/cli-commands/run-scripts').run(sumanConfig, sumanOpts); } else if (tscMultiWatch) { require('./lib/cli-commands/run-tscmultiwatch').run(sumanOpts); } else if (repair) { require('./lib/cli-commands/run-repair').run(sumanOpts); } else if (postinstall) { require('./lib/cli-commands/postinstall').run(sumanOpts); } else{ // etc etc } 如果我尝试一个dynamic加载导入调用,我得到这个: 这显然会返回一个Promise而不是module.exports值。 有没有一种方法来使用dynamic导入语法,而无需asynchronous加载或没有?

TypeScript @types模块无法parsing对方

当我使用npm来安装类似于这样的TypeScript定义时 npm install –save @types/express 我无法使用已安装的模块,因为它们无法解决对方。 例如,@ types / express需要@ types / express-static-server-core,但是@types/express/index.d.ts包含express-static-server-core的相对path,所以无法parsing该模块: node_modules/@types/express/index.d.ts(16,30): error TS2307: Cannot find module 'serve-static'. node_modules/@types/express/index.d.ts(17,23): error TS2307: Cannot find module 'express-serve-static-core'. node_modules/@types/serve-static/index.d.ts(15,26): error TS2307: Cannot find module 'express-serve-static-core'. node_modules/@types/serve-static/index.d.ts(16,20): error TS2307: Cannot find module 'mime'. 我该如何解决这个问题? 什么是安装TypeScript定义的最佳方法? 据我所知,typings已被弃用,所以我试图从@types安装types定义,然后使用 tsc –target ES5 –module commonjs index.ts 但它不工作。 我究竟做错了什么?

TypeScript编译器包含TypeScript定义中不同名称的模块

我有一个简单的Nodejs Typescript项目,通过他们的TypeScript定义文件导入一些JavaScript库。 即: import * as zmq from 'zmq' const sock = zmq.socket("push"); 当我编译这个时,结果是: "use strict"; exports.__esModule = true; var zmq = require("zmq"); var sock = zmq.socket("push"); //# sourceMappingURL=index.js.map 这看起来不错,除了线: var zmq = require("zmq"); 应该是zmq的Node JavaScript模块,即: var zmq = require("../node_modules/zeromq"); 我如何让编译器做这个replace? 我的configuration如下: { "compilerOptions": { "declaration": true, "module": "commonjs", "moduleResolution": "node", "noImplicitAny": true, "outDir": "./lib", […]

Google Maps API的TypeScript 2定义

我正在Visual Studio 2015中使用TypeScript 2,Node JS,Express 4和MongoDB(以Mongoose作为ORM)。我尝试安装Google Maps API库,但是没有成功。 我用“npm install @ google / maps”( https://www.npmjs.com/package/@google/maps )和TypeScript 2定义“npm install @ types / googlemaps”( https:// www .npmjs.com / package / @ types / googlemaps )。 问题是他们有不同的名字! (两者都是官方版本)。 所以编译器告诉我,当我写require(“@ google / maps”)时,找不到模块“@ google / maps”。 我会非常感激,如果你能帮助我,因为我真的需要它为我的项目。 谢谢!

使用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 […]