TYPESCRIPT ARRAY(数组) 技术教程文章

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:如何映射联合数组类型?【代码】

我有以下结构:interface Test1 {number: number; } interface Test2 extends Test1 {text: string; }let test: Test1[] | Test2[] = []; test.map(obj => {}); // does not work我收到错误:Cannot invoke an expression whose type lacks a call signature. Type ‘{ (this: [Test1, Test1, Test1, Test1, Test1], callbackfn: (this: void, value: Test1, index: nu…’ has no compatible call signatures如何映射测试变量?解...

javascript – Typescript递归函数组合【代码】

我想创建一个函数链,它将是一个管道/流/组合函数的输入. 如果没有将类型的字面扩展到选定的深度,这是否可能,这通常是处理? See lodash’s flow. 我想实现链中数据流的类型检查. – 函数的参数是前一个函数的结果 – 第一个参数是模板参数 – 上次返回是模板参数type Chain<In, Out, Tmp1 = any, Tmp2 = any> = [] | [(arg: In) => Out] | [(arg: In) => Tmp1, (i: Tmp1) => Tmp2, ...Chain<Tmp2, Out>];这个想法在草案中. 然而...

javascript – 如何检查数组中是否包含TypeScript中的字符串?【代码】

目前我正在使用Angular 2.0.我有一个数组如下:var channelArray: Array<string> = ['one', 'two', 'three'];如何在TypeScript中检查channelArray是否包含字符串’three’?解决方法:与JavaScript相同,使用Array.prototype.indexOf():console.log(channelArray.indexOf('three') > -1);或使用ECMAScript 2016 Array.prototype.includes():console.log(channelArray.includes('three'));请注意,您还可以使用@Nitzan显示的方法来查...

javascript – TypeScript中包含不同对象的数组【代码】

在从同一个类继承的一个数组中收集各种不同的对象时,如何在TypeScript中设置一个优等的类,以便TypeScript不显示错误? 我正在尝试这样:interface IVehicle{modelName: string }interface ICar extends IVehicle{numberOfDoors: number,isDropTop: boolean }interface IBike extends IVehicle{hasDynamo: boolean }var vehicles: IVehicle[] =[{modelName: "carModelName", // ErrornumberOfDoors: 4,isDropTop: true},{modelName:...

javascript – 为什么数组中的索引在TypeScript中打破了类型的安全性?【代码】

向JavaScript添加静态类型的重点是提供有关类型安全性的一些保证.我注意到数组索引似乎打破了类型安全性而没有使用像任何或非null断言运算符那样的任何脏技巧.let a: Array<number> = [1,2,3,4]; let b: number = a[4]; //undefined此代码不会导致任何TypeScript错误,即使很明显它会违反类型安全性.在我看来,阵列的类型< T>由index []操作的应该是类型T |未定义,但TypeScript编译器将其视为类型T. 经过进一步调查,我发现这种行为也...

javascript – Typescript数组映射vs过滤器vs?【代码】

这是一个打字方法,想要遍历一个字符串数组,并返回另一个字符串数组,其中,匹配regexp的字符串(格式化为“la la la]”将变为“la la la”和字符串不匹配被删除.所以,如果我的输入数组是:"[x]", "x", "[y]"它成为了"x", "y"这是我的代码:questions(): string[] {var regexp = /\[(.*)\]/;return this.rawRecords[0].map((value) => {console.log(value);var match = regexp.exec(value);if (match) {return match[1];}}); }我最终得...

javascript – 从Angular 5中的本地文件到Typescript数组的JSON数组【代码】

我正在使用Angular 5开发一个Web应用程序.我有JSON文件,如下所示:[{"id": 0,"title": "Some title"},{"id": 1,"title": "Some title"},... ]该文件存储在本地.我也有一个界面:export interface Book {id: number;title: string; }问题是我如何: >从文件中获取数据?>从这些数据创建一个Book []类型的数组?解决方法:您可以使用import加载文件:import data from 'path/to/data.json';并将其分配给Book []类型的数组:@Component...

typescript Array添加扩展方法【代码】

declare interface Array<T> {select<T, TR>(this: T[], func: (item: T) => TR): TR[]; }Array.prototype["select"] = function <T, TR>(this: T[], func: (item: T) => TR): TR[] {var vs = new Array<TR>();this.forEach((item) => vs.push(func(item)));return vs;};个人理解,可能有错误 我主要用在单文件,没有声明模块的项目中,其他的可能有区别 接口的declare关键字的意思是扩展原有的(或者说全局作用域中的?)接口,否...