javascript与typescript

以下是为您整理出来关于【javascript与typescript】合集内容,如果觉得还不错,请帮忙转发推荐。

【javascript与typescript】技术教程文章

javascript-Typescript中的for-in循环始终将变量视为字符串【代码】

这个问题已经在这里有了答案: > for … in loop with string array outputs indices 4个这已经困扰了我一段时间了.在Typescript中,如果我定义一个For-in循环,则我的变量始终被视为字符串.例:for(var room in this.rooms) {room.placement.x = 52; }“ room.placement.x”将失败,因为它将“ room”视为字符串. “ this.rooms”实际上是Room对象的集合,但是room不是Room...

javascript-TypeScript在Bluebird中使用ES5中的动态导入【代码】

我正在尝试在TypeScript中使用新的动态import()函数,但是出现以下错误:TS2712: A dynamic import call in ES5/ES3 requires the ‘Promise’constructor. Make sure you have a declaration for the ‘Promise’constructor or include ‘ES2015’ in your --lib option.我可以像消息提示那样将ES2015.promise lib包含在我的tsconfig中,但这会使我在使用Bluebird Promise时失去类型安全性. 我知道可以在TypeScript中将Bluebird用...

JavaScript-Typescript:属性不存在【代码】

我正在尝试为Typescript中的REST Api接口开发装饰器.这是装饰器的实现export function RemoteResource(params: any): Function {console.log("RemoteResource.params: ", params);return function (target: Function) {//--POSTtarget.prototype.post = function () {console.log("----POST");};//--GETtarget.prototype.retrieve = function () {console.log("----GET");};//--DELETEtarget.prototype.remove = function () {cons...

javascript-Typescript和AngularJS 1.5:如何处理导出类【代码】

我有这个module.ts文件:import { IHttpService, IPromise } from 'angular';export class ProductService {static $inject = ["$http"];constructor(private $http: IHttpService) { }loaded: boolean = false;promise: IPromise<{ data: any }>;getProducts(): IPromise<{ data: any }> {if (!this.loaded) {this.loaded = true;this.promise = this.$http.get('/products.json');}return this.promise;} }var module = angular....

javascript-Typescript-括号插入对象不会产生错误【代码】

我发现TypeScript允许我使用方括号表示法来在只有键的情况下通过索引访问对象.例如:interface testObject {name: string;id: number; }let first: testObject = {name: "Marquizzo", id: 1};let second = first[1]; // <-- Should yield error!first [1]应该给我一个错误,因为在testObject接口中未将1定义为有效键.我是否可以打开一个标志来避免这种情况发生?解决方法:听起来您没有将noImplicitAny compiler option设置为true. 如...

Javascript-在Typescript中,有什么方法可以将类编写为数组,所以我可以做class [i],就像C#中的List【代码】

我是一位从C#开始的新游戏开发人员. 现在我需要将我的游戏之一转移到打字稿上. 我试图用我在C#中非常熟悉的打字稿自定义列表.我的代码如下:export class List {private items: Array; constructor() {this.items = []; }get count(): number {return this.items.length; }add(value: T): void {this.items.push(value); }get(index: number): T {return this.items[index]; } contains(item: T): boolean{if(this.items.indexOf(i...

javascript-在Typescript中使用Object.keys时避免隐式“ any”类型【代码】

我有一个带有已键入键的对象,并且我想遍历它并保持其类型,避免“元素隐式具有“任何”类型,因为“ ContactList”没有索引签名”. 我知道在Typescript的GitHub页面上对此进行了详尽的讨论,但是我在这里找不到任何解决方案.尝试用枚举和联合,但我仍然收到错误. 枚举(playground)的示例enum Names {Joe,Bill,Bob }type ContactList = {[key in Names]: {isFriend: boolean;} }const contactList: ContactList = {[Names.Joe]: {isFrie...

为什么要在Javascript / Typescript上进行多种导入/导出【代码】

我是服务器端javascript的新手,以前我曾将nodejs用于简单的事情,但仅使用默认库(在此我永远不需要使用require或import关键字),但是最近我正在学习ReactNative/ReactXP我已经看到:import RX = require('reactxp'); const popsicle = require('popsicle'); import LoginPage = require("./LoginPage"); import React, { Component } from 'react'; import { AppRegistry, Text } from 'react-native'; import AppState from './AppS...

javascript-Typescript-索引表达式参数的类型必须为’string’,’number’,’symbol’或’any’【代码】

我正在使用打字稿1.7.5,并且遇到以下情况,索引表达式参数必须为’string’,’number’或’any’类型的错误:const settings: any = {};_.forEach(data, (d, name: string) => { //data is just an objectsettings[name] = {};const colors = ColorGenerator.generateColors(Object.keys(d.ch).length);_(d.ch).keys().zip(colors).forEach(([channel, color]) => {// name and channel are both stringssettings[name][channel] = ...

javascript-在TypeScript中将JSON响应映射到组件模型.【代码】

我有一个返回JSON字符串的Angular 2应用程序.我想获取该JSON字符串并将其值映射到模型. 据我了解,TypeScript模型文件应该有助于将HTTP Get响应映射到对象-在我的情况下是一个名为’CustomObject’的类.这是我为TypeScript识别而创建的TypeScript模型文件:export class CustomObject {public Name: string; }这是我的JSON字符串的样子(从Chrome开发者工具稍作修改以消除不必要的变量):"{"EventType": 3,"Description": "Test Desc...