Tag: typescript

为什么在有顶级常量时,Typescript会出错,或者在Node.js的许多文件中让相同的名字出现

为什么在顶层声明一个const或let导致TS2451错误(见下文)? 我明白,在浏览器中,所有的脚本共享顶级范围,但我正在写所有模块包装的 Node.js。 这种情况下的错误是否与Node.js相关? 如果不是,可以configuration了/configuration我的问题? 为什么var不会发生同样的错误? 是不是在顶层的范围接近var,const和let是相同的? a.ts : let lme = 'A'; const cme = 'A'; var me = 'a'; b.js : let lme = 'A'; const cme = 'A'; var me = 'a'; tsconfig.json : { "compilerOptions": { "module": "commonjs", "target": "es6", "noImplicitAny": false, "sourceMap": false }, "exclude": [ "node_modules" ] } 输出 […]

使用声明文件打字稿

您好我有我的打字稿项目中使用声明文件的问题。 声明文件如下: // Type definitions for Dropzone 4.3.0 // Project: http://www.dropzonejs.com/ // Definitions by: Natan Vivo <https://github.com/nvivo>, Andy Hawkins <https://github.com/a904guy/,http://a904guy.com/,http://www.bmbsqd.com>, Vasya Aksyonov <https://github.com/outring>, Simon Huber <https://github.com/renuo> // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// <reference path="jquery.d.ts"/> interface DropzoneResizeInfo { srcX?:number; srcY?:number; trgX?:number; trgY?:number; srcWidth?:number; srcHeight?:number; trgWidth?:number; trgHeight?:number; optWidth?:number; optHeight?:number; } interface DropzoneFile extends File { previewElement: HTMLElement; previewTemplate: HTMLElement; […]

属性“服务器”不存在于typeof“typeof”http“'

我知道var someModule = require('someModule')通常被import * as someModule from 'someModule'replaceimport * as someModule from 'someModule'但我无法弄清楚如何使用Typescript / ES6语法来表示下面的Node.js代码: var server = require('http').Server(app); 读取导入后,用es6调用函数,我尝试了以下方法: import * as httpModule from 'http'; const server = httpModule.Server(app); 和代码编译和运行正常,但我仍然得到这个TS错误: [ts] Property 'Server' does not exist on type 'typeof "http"' 。 我有@ types / node和@ types / express安装。 我错过了什么吗?

在Typescript中将定义添加到现有模块中

我正在努力与Typescript和修改现有模块的定义。 我们习惯于把所有我们想要输出的东西放到“res.out”中,最后还是有这样的“res.json(res.out)”。 这使我们能够在发送响应的时候对应用程序进行全面控制。 所以我有这样的function export async function register(req: Request, res: Response, next: Next) { try { const user = await userService.registerOrdinaryUser(req.body) res.status(201); res.out = user; return helper.resSend(req, res, next); } catch (ex) { return helper.resError(ex, req, res, next); } }; 我们正在使用restify。 我得到编译错误,因为“out”不是restify.Response的一部分。 现在我们有了解决方法,我们有我们自己的“对象”,扩展了Restify的对象。 import { Server as iServer, Request as iRequest, Response as iResponse, } […]

在Nodejs中跨多个文件的Typescript内部模块

我一直在通过StackOverflowpost和多个博客文章,但似乎无法find答案。 我正在使用VS Code,nodejs,typescript和gulp。 当我把我所有的模块控制器代码放入一个这样的文件: <controllers.ts> module Controllers { export class Api { get(controllerName:string):IController { } } } module Controllers { export interface IController { performAction(actionName:string):any; } } module Controllers { export class ControllerBase implements IController { performAction(actionName:string):any { } } } module Controllers { export class UserController extends ControllerBase { } } export = Controllers; […]

用于cloudboost的打字稿(和ion2一起使用)

我想知道任何可用的typescloudboost.io https://github.com/CloudBoost/JavaScriptSDK 。 我没有为现有的js模块写types的经验。 我试图准备任何最愚蠢的d.ts但没有成功。 即使用ionic2和npm install cloudboost (目前也需要单独npm install fs )我正在尝试使用cloudboost.d.ts 。 declare module "cloudboost" {} 要么 declare var CB: any; declare module "cloudboost" { export var CB: any; } 结果是奇怪的,因为cloudboost包含到ionic2构build(使用它的webpackconfiguration),但是当使用ie import * as CB from "cloudboost"; 或类似的import变体,即使内部webpack调用似乎指向适当的cloudboost代码,也会返回一些socket.io对象的CB值。 所以即使any愚蠢的东西都会让我满意。

Typescript:如何parsingnode.js的绝对模块path?

我需要基于baseUrl来parsing模块,所以输出的代码可以用于node.js 这是我的src/server/index.ts import express = require('express'); import {port, databaseUri} from 'server/config'; … 这是我的src/server/config/index.ts export const databaseUri: string = process.env.DATABASE_URI || process.env.MONGODB_URI; export const port: number = process.env.PORT || 1337; 运行tsc我能够编译所有文件没有erros,但输出: dist/server/index.js是 "use strict"; var express = require("express"); var config_1 = require("server/config"); … 导致与如果我试图与node dist/sever/index.js使用它Cannot find module 'server/config' 。 为什么server/configpath没有以任何方式解决,所以它可能会使用编译代码或如何使其解决它。 或者我在做什么,或者想错了什么? 我的tsc –version是2.1.4 这是我的tsconfig.json : { […]

如何在meteor中使用打字稿使用mongo模式validation?

在使用Typescript时,是否有一个在Meteor 1.3中使用模式validation的包? meteor指南推荐的软件包(aldeed:simple-schema)似乎没有定义文件。 那么用什么来代替,或者Typescript有一个内置的方法来做到这一点?

MongDB找不到Promisetypes定义

我创build了一个新的项目,安装了几个types定义(来自DefinitelyTyped),并创build了一个基本的src/go.ts文件。 每当我编译代码时,我会得到大量的TS2304: Cannot find name 'Promise'错误! ./typings/mongodb/index.d.ts引用Promise ,但编译器似乎没有find。 请注意,我也安装了dt〜promise。 截图 (左侧的错误,安装的types定义和应用程序代码在右侧) typings.json内容: { "globalDependencies": { "dotenv": "registry:dt/dotenv#2.0.0+20160327131627", "express": "registry:dt/express#4.0.0+20160708185218", "express-serve-static-core": "registry:dt/express-serve-static-core#4.0.0+20160805091045", "mime": "registry:dt/mime#0.0.0+20160316155526", "mongodb": "registry:dt/mongodb#2.1.0+20160602142941", "mongoose": "registry:dt/mongoose#4.5.4+20160807120805", "mongoose-promise": "registry:dt/mongoose-promise#4.5.4+20160720003345", "node": "registry:dt/node#6.0.0+20160807145350", "promise": "registry:dt/promise#7.1.1+20160602154553", "serve-static": "registry:dt/serve-static#0.0.0+20160606155157" } } 有什么想法吗?

结合Visual Studio代码构build任务

Visual Studio代码文档提供了示例tasks.jsonconfiguration ,允许打字稿编译或降价编辑。 它并没有阐明如何同时实现两者 。 如何做到这一点? 这是两个例子的总结 打字稿例子 如果我希望VSCode执行打印脚本构build步骤,说明我需要安装打字稿( npm install -g typescript打印脚本),然后定义以下任务: { "version": "0.1.0", "command": "tsc", "isShellCommand": true, "showOutput": "silent", "args": ["*.ts"], "problemMatcher": "$tsc" } Markdown示例 如果我希望VSCode执行Markdown构build步骤,文档说我可以安装我select的markdown插件(例如npm install -g marked ),然后定义一个任务: { "version": "0.1.0", "command": "marked", "isShellCommand": true, "args": ["sample.md", "-o", "sample.html"] } 怎么办? 显然,tasks.json可能只包含一个JSON对象。 因此,我不能简单地用逗号将上述两个定义join。 另一方面,可以在整个任务定义中定义多个任务: { "version": "0.1.0", "command": "<what goes […]