Tag: 打字稿

使用typecrit / mocha进行unit testing时找不到模块

对于Typescript,我是一个相当新的东西,并试图在本教程之后设置一个项目: http ://mherman.org/blog/2016/11/05/developing-a-restful-api-with-node-and-typescript/ 我正在创build一个名为helloworld.test.ts的文件,并运行npm test来查看api的基本路线是否正常工作。 但是,当我运行npmtesting命令(哪些做: mocha –reporter spec –compilers ts:ts-node/register 'test/**/*.test.ts' )我得到以下错误: /home/louis/Bureau/my-simple-project/node_modules/ts-node/src/index.ts:312 throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset)) ^ TSError: ⨯ Unable to compile TypeScript Cannot find type definition file for 'body-parser'. (2688) Cannot find type definition file for 'chai'. (2688) Cannot find type definition file for 'chai-http'. (2688) Cannot find type definition […]

每次运行不同的入口点的Webpack,但仍然使用configuration文件?

我有一个项目,其中包含众多不同网站的代码。 这些不是我build立或维护的网站,我只是为他们构build组件,并提供这些代码在网站上实施。 文件夹结构如下所示: . +– tsconfig.json +– site_A | +– component1 | | +– component1.html | | +– component1.css | | +– component1.js +– site_B | +– component2 | | +– component2.ts +– my_modules | +– module1.ts | +– module2.ts | +– module3.ts my_modules文件夹包含我经常在许多组件中使用的函数,因此我想将它们编写为可重用模块。 我的问题是,因为这不是一个单一的项目,我不能真正运行一个单一的构build脚本,但实际上每次运行Webpack时都需要指定入口点。 我目前使用的解决scheme是以下npm构build脚本: "build": webpack –module-bind 'js=babel-loader' –module-bind 'ts=ts-loader' –output-path './build' –output-filename […]

在NextJs中访问服务器端代码中的静态资源?

我使用NextJS的静态渲染function来生成我的网站的静态版本,因此我想确保在页面的第一次呈现所有需要正确呈现的数据提供。 我有一些博客文章,我已经存储为.md文件在/静态,并希望访问他们在一个页面中,如: import * as fs from "fs"; … export default class extends React.Component<IProps, any> { static async getInitialProps (props: IServerProps) { const post = (await getDb()).posts.find(p => p.id == props.query.id); const markdown = fs.readFileSync(`/static/posts/${post.markdownFileName}`); return { post, markdown } } … 但是,如果尝试运行上面的,我得到以下错误: This dependency was not found: * fs 所以我不知道我应该如何去访问这些静态资源,而在服务器上..

如何使用npm将PDF.JS导入到Typescript中

我安装了PDF.JS npm软件包,并将其导入打字稿文件中,但不能按预期工作。 我使用下面的代码将PDF.JS导入打字稿。 import { PDFJSStatic } from 'pdfjs-dist'; const PDFJS: PDFJSStatic = require('pdfjs-dist'); PDFJS.getDocument('helloworld.pdf').then(console.log); 我得到的错误 严重级代码说明项目文件行抑制状态错误TS2307找不到模块“pdfjs-dist”。 app.ts 2活动的严重程度代码说明项目文件行抑制状态错误TS2304找不到名称'require'。 app.ts 3 Active 请提供您宝贵的build议,让我知道我错过了什么,如果有的话。

打字稿冲突的variables名称

我试图编译一些Node.js打字稿的代码,但我遇到了两个名为consolevariables的问题。 本质上,Node.js有一个console.trace()方法,而正常的浏览器端JavaScript不。 我在我的Typescript文件中引用了node.d.ts,其他一切正常。 node.d.ts将console声明为一个variables,但是lib.d.ts也是如此,这似乎是在重写Node.js版本。 将–nolib添加到编译器只会引发数百个关于未定义符号的错误。 有没有办法解决这个问题,而不编辑lib.d.ts? (或者控制台: (<any>console).trace() ?)

在Typescript Module Nodejs中导出所有内容

我只想导出一个Typescript模块内的所有东西,例如我声明一个模块是这样的: /// <reference path='../d.ts/DefinitelyTyped/node/node.d.ts' /> /// <reference path='../d.ts/DefinitelyTyped/express/express.d.ts' /> /// <reference path='../d.ts/DefinitelyTyped/mongoose/mongoose.d.ts' /> import express = require("express"); import mongoose = require("mongoose"); export module Users { export var users: Express = express(); export var base_URL: string = "/users"; users.get(base_URL, (req, res) => { res.render("index", { title: "Cheese cakes" }); }); } 现在,为了访问base_URL和users ,您可以看到,我也需要明确地导出它们。 我可以做什么说,我想要导出一个模块内的一切。

是否有可能在打字稿中使用nodejs风格的模块?

在节点中,我可以通过设置exports对象的属性来定义一个这样的模块: module.js exports.fun = function (val) { console.log(val); }; 并使用var module = require('module')来请求它并使用module.fun()函数。 是否可以像这样在TypeScript中定义模块: module.ts exports.fun = function (val :string) { console.log(val); }; 然后使用类似语法的节点(比如import module = require('module.ts')将其导入到其他某个文件中,以便将其编译为nodejs,但是如果现在在某些.ts文件中使用module.fun()它应该给我一个错误,如果参数不符合module.ts文件中指定的types。 我怎样才能在打字稿中做到这一点?

TypeScript是否需要node.js运行?

安装TypeScript需要node.js. 我假设TypeScript使用Node.js将.ts编译为.js文件。 我的问题是,这是否创build.js文件需要node.js? 到目前为止我所看到的那些似乎不是。 如果不使用,我不想将node.js加载到我的html页面中。 谢谢 – 戴夫

如何将第三方Javascript文件导入到IntelliJ中,以便我可以在一个Typescript文件中引用它

我正在尝试在IntelliJ中编写Typescript,不知道如何告诉IntelliJ“导入”一些第三方的Javascript文件。 IntelliJ(或者是Node.JS?)给出了以下的抱怨: C:/Temp/Typescript Example Project/ts/FinancialService.ts(2,17): error TS2095: Could not find symbol 'com'. C:/Temp/Typescript Example Project/ts/FinancialService.ts(4,31): error TS2095: Could not find symbol 'com'. 我想'导入' Thirdparty.Calculator.js : var com = com || {}; com.thirdparty = com.thirdparty || {}; com.thirdparty.Calculator = function() { this.add = function(a, b) { return a + b; }; this.square = function(n) { return […]

TypeScript使用typescript-require共享文件

我使用TypeScript来编写我的JavaScript文件与面向对象编程。 我想使用节点模块https://npmjs.org/package/typescript-require从其他文件中获取我的.ts文件。 我想在服务器和客户端共享我的文件。 (浏览器)这非常重要。 请注意,文件夹/共享/并不意味着客户端和服务器之间,而是游戏服务器和Web服务器之间共享。 我使用pomelo.js作为框架,这就是为什么。 目前我没有使用(成功)打字稿需要库。 我喜欢这样做: 共享/ LIB / message.js var Message = require('./../classes/Message'); module.exports = { getNewInstance: function(message, data, status){ console.log(requireTs);// Global typescript-require instance console.log(Message); return new Message(message, data, status); } }; 这个文件需要Message.js来创build新的实例。 共享/类/消息。 TS class Message{ // Big stuff } try{ module.exports = Message; }catch(e){} 在fil的最后,我添加这个try / catch来将类添加到module.exports(如果存在)。 (它工作,但它不是一个真正的好办法,我想做得更好)如果我从浏览器加载文件,module.export将不存在。 所以,我上面做的是工作。 现在,如果我尝试使用打字稿需求模块,我会改变一些事情: […]