Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 通过扩展现有类型创建新的Typescript数据类型_Arrays_Typescript - Fatal编程技术网

Arrays 通过扩展现有类型创建新的Typescript数据类型

Arrays 通过扩展现有类型创建新的Typescript数据类型,arrays,typescript,Arrays,Typescript,我想通过扩展现有的数组来创建一个新的数据类型 以下是一个扩展示例: interface Array<T> { Count(): number; } Array.prototype.Count = function() { return this.length; } 以下工作: interface IItem { name: string } let items = Array<IItem> = []; // ad

我想通过扩展现有的
数组
来创建一个新的数据类型

以下是一个扩展示例:

interface Array<T> {
    Count(): number;
}

Array.prototype.Count = function() {
    return this.length;
}
以下工作:

 interface IItem {
        name: string
   }

   let items = Array<IItem> = [];

   // add few 'item's to items.    

   let list = new List<IItem>(items);
   let filter = (item: IItem, name: string) => { return item.name === name };
   let filteredList = list.where(filter, name);
接口项目{
名称:string
}
让items=Array=[];
//向项目中添加少量“项目”。
let list=新列表(项目);
let filter=(item:IItem,name:string)=>{return item.name==name};
let filteredList=list.where(过滤器,名称);
要使其正常工作,请执行以下操作:

let list = List<IItem> = [];

// add few 'item's to items.    

let filter = (item: IItem, name: string) => { return item.name === name };
let filteredList = list.where(filter, name);
let list=list=[];
//向项目中添加少量“项目”。
let filter=(item:IItem,name:string)=>{return item.name==name};
let filteredList=list.where(过滤器,名称);
编译器错误:

[ts] Type 'undefined[]' is not assignable to type 'List<IItem>'.
Property '_items' is missing in type 'undefined[]'.
[ts]类型“未定义[]”不能分配给类型“列表”。
类型“undefined[]”中缺少属性“\u items”。

我遗漏了什么吗?

ES2015(又名ES6/Harmony)附带了内置类型的子分类功能,但目前没有浏览器默认启用该功能(请参阅)


你现在唯一的选择是使用一些原型黑客…

你可以用一种现在有点黑客的方式来做这件事,但这将让你在对本机类型进行子分类时轻松地替换实现

设置看起来像这样

interface IList<T> extends Array<T> {
    count(): number;
}


var ListShim = function() {
    this.count = () => {
        return this.length;
    }
}
ListShim.prototype = new Array();

class List<T> {
    static create<T>() : IList<T> {
        return <IList<T>> new ListShim();
    }
}
接口IList扩展数组{
count():数字;
}
var ListShim=函数(){
this.count=()=>{
返回这个.length;
}
}
ListShim.prototype=新数组();
班级名单{
静态创建():IList{
返回新的ListShim();
}
}
你是这样使用它的:

var list = List.create<string>();

list.push('test a');
console.log(list.count());
console.log(list[0]);

list.push('test b');
console.log(list.count());
console.log(list[1]);
var list=list.create();
list.push(“测试a”);
console.log(list.count());
console.log(列表[0]);
列表。推送(“测试b”);
console.log(list.count());
console.log(列表[1]);
稍后,如果您发现可以对本机类型进行子类化(即所有浏览器都允许),则可以替换实现,而无需更改调用代码

interface IList<T> extends Array<T> {
    count(): number;
}

class List<T> extends Array<T> {
    static create<T>() : IList<T> {
        return new List();
    }

    count() {
        return this.length;
    }
}
接口IList扩展数组{
count():数字;
}
类列表扩展了数组{
静态创建():IList{
返回新列表();
}
计数(){
返回这个.length;
}
}

最后一个代码块是“黑暗中的刺刀”,因为我不知道它到底是什么样子,但您应该通过
create
factory方法与确切的细节隔离开来。

太好了!像梦一样工作。。。一个愚蠢的问题:为什么不能用
var ListShim=()=>…
替换
var ListShim=function()…
@Steve@FlippieCoetser因为它将使用在函数外部找到的
this
的值-
this
将不等于实例。@Sohnee,有什么建议吗?见上文更新部分。
interface IList<T> extends Array<T> {
    count(): number;
}

class List<T> extends Array<T> {
    static create<T>() : IList<T> {
        return new List();
    }

    count() {
        return this.length;
    }
}