Tag: 打字稿

如何在打字稿中强制variables的types?

我试图写一些函数,将mongooseUser模型转换为一个带有项目符号的string: // Simplified version so you get the idea interface IUser { name: string } function userDetails (user: IUser, keys: string[]): string { return keys.map((k: string): string => { return `- ${k} : ${user[k]}` }) .join('\n') } 但是我有一个奇怪的编译器错误, user[k]下划线: 对象types的索引签名隐式地具有“任何”types。 有没有办法“强制”打字机pipe理该user[k]是一个string? 我尝试user[k] as string或<string> user[k]没有成功。 另外,如果我从返回的string中删除${user[k]} ,那么编译器会停止抱怨 从编译器的错误公寓,一切工作在运行时。 谢谢 !

Angular2 + TypeScript + moment.js => locale并不都在那里(只是'en')

我正在通过一本书教程学习TypeScript和AngularJS 2.0 :(Become_a_Ninja_with_Angular2)。 在某个时候,它解释了如何制作自己的Pipe,并通过moment.js的实现。 在我的项目所在的文件夹我做的CLI: npm install moment (通知:本书还告诉做typestypings install –save –ambient moment-node ,但即使我改变抛出一个错误 – –ambient –global ,这个错误碰巧不是使用moment.js的问题,因为我在下面描述的代码的其余部分运行)。 然后,由于以前的CLI,它会在我的项目文件夹下创build: [my project folder]\node_modules\moment 然后在[my project folder]\main.html ,我有一个<script>标签:` <script> System.config({ defaultJSExtensions: true, map: { … [plenty of stuff starting with @angular].. '@angular/platform-browser-dynamic':'node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js', 'rxjs': 'node_modules/rxjs', 'moment':'node_modules/moment/moment' } }); System.import('main'); </script> 我的自定义pipe道如下所示: import { PipeTransform, Pipe } from '@angular/core'; […]

将属性添加到node.js中的嵌套接口

在一个名为“some-file.ts”的文件中,我想这样做: global.someProp = someVar; 而global是在node.d.ts中定义的NodeJS.Globaltypes的预定义对象。 我怎样才能使这个接口部分? 我已经尝试了以下所有无效: interface Global { someProp: string; } global.someProp = someVar; interface NodeJS.Global { someProp: string; } global.someProp = someVar; namespace NodeJS{ interface Global { someProp: string; } } global.someProp = someVar; 我不断收到: TS339:“全局”types中不存在属性“someProp” 我该如何解决?

包括TypeScript编译上下文中的app.component.ts

我已经build立了一个Angular2项目跟随Angular2文档当我想从@angular/core打字稿导入Component给我这个错误 TypeScript编译上下文中不包含文件“C:/sendbox/mean/client/app/app.component.ts”。 如果不是这样,请检查您的tsconfig.json文件的“files”或“filesGlob”部分。第1行col 1

避免types“{}”不可分配为键入“X”

我正在将JavaScript项目迁移到TypeScript。 在TS中使用诸如URL之类的节点模块对我造成一些麻烦: import nodeUrl = require('url'); // … // this worked fine in JS nodeUrl.format({ // just for demonstration x: this.getX(someObj), y: this.getY(someObj) }); 结果是: 键入“{}”不能分配给“string” 这是由于该模块function的定义。 从@ types / node / index.d.ts : declare module "url" { export interface Url { href?: string; protocol?: string; auth?: string; hostname?: string; port?: string; host?: string; pathname?: […]

错误TS2307:无法find模块“应用程序”

我想在节点端使用打字稿。 我有一个非常简单的服务器。 我的服务器文件夹里面的tsconfig.file如下: { "compilerOptions": { "module": "commonjs", "target": "es5", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "outDir": "../dist/serverBuild", "typeRoots": [ "../node_modules/@types" ] }, "exclude": [ "../node_modules" ] } 我有一个app.ts文件,其中有明确的相关configuration,然后我有server.ts文件,这是从app.ts导入应用程序模块,它有创build和启动节点服务器的代码。 但是我得到以下错误: TSError:⨯无法编译TypeScript server.ts(11,22):找不到模块'app'。 (2307) 其他模块,我在我的server.ts文件中导入像http模块不会抛出任何这样的错误。 我在这里做错了什么。 以下是我如何导入模块: import * as http from "http"; import * as app from "app"; 谢谢!

如何在Typescript中覆盖属性是不可空的

Node内置的IncomingMessage(参数(req, res, next)中的(req, res, next)types)的DefinitelyTyped定义已将url定义为可空 。 以下是定义文件的剪切部分: // @types/node/index.d.ts declare module "http" { export interface IncomingMessage { /** * Only valid for request obtained from http.Server. */ url?: string; } } 正如注释所说,这是因为这个属性只有在你从http.Server获得这个IncomingMessage的实例时才有效。 在其他用途​​中,它不会存在,因此它是可以空的。 然而,在我的情况下, 我知道我只是从http.Server获得这些实例,所以有点烦人,我不能没有额外的卫兵访问属性。 import { IncomingMessage, ServerResponse } from 'http'; function someMiddleware(req: IncomingMessage, res: ServerResponse, next: Function) { const myStr: string = req.url; […]

在打字稿中parsingXML

使用Typescript 2.0(tsc版本2.0.3)。 我没有成功尝试在angular2 / typescript应用程序中使用xml2js节点库(请参阅https://www.npmjs.com/package/xml2js )。 似乎很清楚,我不熟悉SystemJS设置库使用的方式。 要安装xml2js库,我做了以下操作: npm install –save xml2js npm install –save @types/xml2js 这里是相关的文件: tsconfig.json: { "compilerOptions": { "target": "es5", "module": "commonjs", "moduleResolution": "node", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, "noImplicitAny": false, "outDir" : "build" } } systemjs.config.js /** * System configuration for Angular samples * Adjust as necessary for […]

npm @types软件包的typesglobalDevDependencies的等价性是什么?

我想升级一个tsc@1.8项目到TSC @ 2,并在我的工具链中进行沟通types。 这对于来自typings.json这些依赖关系的常见依赖不是问题: "dependencies": { "bluebird": "registry:npm/bluebird#3.3.4+20160515010139", "lodash": "registry:npm/lodash#4.0.0+20160416211519", "mime": "registry:npm/mime#1.3.0+20160423043021" } 我可以很容易地安装通过 npm i @types/bluebird @types/lodass @types/mime 然而,我的globalDevDependencies也有一些globalDevDependencies于我的testing设置: "globalDevDependencies": { "mocha": "registry:dt/mocha#2.2.5+20160317120654" } 我的第一个尝试是: npm install @types/mocha –save-dev 然而,现在呢,他们抱怨说,它并不了解it describe的mochafunction。 tests/unit/HelloServiceTest.ts(4,1): error TS2304: Cannot find name 'describe'. tests/unit/HelloServiceTest.ts(5,5): error TS2304: Cannot find name 'it'. tests/unit/HelloServiceTest.ts(10,5): error TS2304: Cannot find name 'it'. 我错误地认为在全球安装这些解决scheme可能会解决这个问题: npm […]

如何使用快递打字稿

所以定义文件从tsd到typings,现在到@types,我怎么能在节点/快递项目中使用@types,这个时候什么是首选,也是为什么我们从tsd转到打字和现在@types 谢谢,