Tag: 明确

DefinitelyTyped如何将声明文件绑定到JavaScript库?

我想知道如何将DefinitelyTyped每个声明types绑定到其相关的JavaScript库。 例如@type/lodash如何链接到js库lodash ? 手册指出我们可以在npm包中的package.json中添加一个"type"标记,但是在search了几个JavaScript组件(包括angular , ace等)之后我没有看到。 精通TypeScript的人能告诉我这个窍门吗? 谢谢!

我如何得到这个Typescript Webpack项目编译没有错误?

我试图将我的工作webpack node.js项目中的两个.js文件转换为.ts文件并编译它(actions.ts和flux.ts)。 运行时 webpack –progress –colors 我得到这个错误: 错误在./src/app/tools/flux.ts (2,11):错误TS2304:找不到名称'require'。 错误在./src/app/tools/flux.ts (4,1):错误TS2304:找不到名称“模块”。 错误在./src/app/actions/actions.ts (2,12):错误TS2304:找不到名称'require'。 错误在./src/app/actions/actions.ts (11,1):错误TS2304:找不到名称“模块”。 我该如何解决这些问题? 在这个屏幕截图中,您可以看到我的文件夹结构以及我正在尝试编译的代码: 这是我的webpackconfiguration,如果有帮助: 'use strict'; var webpack = require('webpack'); var HtmlWebpackPlugin = require('html-webpack-plugin'); var CopyWebpackPlugin = require('copy-webpack-plugin'); var path = require('path'); var rootPath = __dirname; //site var srcPath = path.join(rootPath, 'src'); //site/src module.exports = { bail: true, cache: true, context: […]

如何在节点应用程序中使用@ types / node

我在Ubuntu 16.04上的VSCode工作。 我使用下面的逗号创build了节点项目: npm init tsc –init 我创build了一个名为index.ts的新文件。 我正在尝试使用fs和readling来读取文件内容。 但是当我在index.d.ts的顶部写下代码行时 : import fs = require('fs'); import readline = require('readline'); 我得到下面的错误: 找不到模块'FS' , 无法find模块'readline' 甚至没有find过程 。 我使用下面的命令从这里安装节点的types: sudo npm install @types/node -global –save 任何人都可以请帮助我如何解决这个错误?

如何为代替“exports”对象的模块创buildTypescript(1.8)types定义?

我想创build一个模块的types定义,用一个匿名函数replacemodule.exports。 所以,模块代码这样做: module.exports = function(foo) { /* some code */} 要在JavaScript(Node)中使用模块,我们这样做: const theModule = require("theModule"); theModule("foo"); 我已经写了一个.d.ts文件这样做: export function theModule(foo: string): string; 然后我可以像这样写一个TypeScript文件: import {theModule} from "theModule"; theModule("foo"); 当我编译成JavaScript时,我得到: const theModule_1 = require("theModule"); theModule_1.theModule("foo"); 我不是模块作者。 所以,我不能改变模块代码。 如何编写我的types定义,以便它正确地转换为: const theModule = require("theModule"); theModule("foo"); 编辑:为了清晰起见,基于正确的答案,我的最终代码如下所示: 该-module.d.ts declare module "theModule" { function main(foo: string): string; export = main; […]

“肯定键入的”NodeJS + Express定义文件有错误

我正在尝试将具有NodeJS + ExpressJS的项目转换为打字稿。 我从Definitely Typed获得了定义文件,但是他们似乎有很多错误: export interface ClientRequest extends events.NodeEventEmitter, stream.WritableStream { // Extended base methods write(str: string, encoding?: string, fd?: string): boolean; write(buffer: NodeBuffer): boolean; write(chunk: any, encoding?: string): void; end(data?: any, encoding?: string): void; abort(): void; setTimeout(timeout: number, callback?: Function): void; setNoDelay(noDelay?: Function): void; setSocketKeepAlive(enable?: boolean, initialDelay?: number): void; } export interface Server […]

如何在@types上为范围/命名空间包安装TypeScript声明?

我正在使用TypeScript,我想要使用不包含自己的types声明的作用域包(例如@foo/bar , @babel/core等)。 我试图运行类似的东西 npm install @types/@foo/bar 但似乎并不可用。 有没有办法从@types作用域DefinitelyTyped这些.d.ts文件? 如果需要的话,有没有办法编写自己的作用域包?

Express.js:如何在req.param中创buildapp.get('/ i'..)?

我使用nodejs 0.8.21和3.1.0 我需要从像http://mysite.com/39i这样的http://mysite.com/39i读取userId 。 这意味着userId=39 。 怎么做? 谢谢。 样品: app.get('/:userId_i', function(req, res) { }); app.param('userId', function(req, res, next, id){ });

如何在TypeScript中正确地要求和声明节点库types?

我正在尝试使用标准节点库(例如fs , path , stream , http在TypeScript中为节点编写一个包。 当我尝试在.ts文件中导入库时,VS Code标记相应的行并出现错误: [ts] Cannot find module 'fs' 。 无论如何尝试导入库,都会发生这种情况: import * as fs from 'fs'; // [ts] Cannot find module 'fs' import fs = require('fs'); // [ts] Cannot find module 'fs' const fs = require('fs'); // [ts] Cannot find name 'require' 我(应该)有安装正确的定义安装 – typings install –save –ambient node […]