【问题标题】:Declare TypedArray with ArrayLike?用 ArrayLike 声明 TypedArray?
【发布时间】:2016-01-24 18:06:26
【问题描述】:

我正在寻找一种在 d.ts 文件中声明泛型 TypedArray 类型的方法。 TypedArray 似乎由于某种原因不存在,但我在某处发现了一条建议使用 ArrayLike<T> 的评论。这仍然是最好的解决方案吗?

我可以猜出ArrayLike 是什么,但有任何相关文档吗?谷歌搜索和搜索 Typescript 网站的次数并不多。

编辑:我刚刚注意到类型化数组构造函数被声明为将 ArrayLike 作为第一个参数,因此这表明这是正确的方法。

【问题讨论】:

    标签: typescript


    【解决方案1】:

    我不确定我是否正确理解了您的问题,但以下实例在 TypeScript 1.6(撰写本文时的最新稳定版本)中有效:

    let t01 = new Uint8Array([1, 2, 3, 4]);
    let t02 = new Int8Array([1, 2, 3, 4]);  
    let t03 = new Uint8Array([1, 2, 3, 4]);
    let t04 = new Uint8ClampedArray([1, 2, 3, 4]);
    let t05 = new Int16Array([1, 2, 3, 4]);
    let t06 = new Uint16Array([1, 2, 3, 4]);
    let t07 = new Int32Array([1, 2, 3, 4]);
    let t08 = new Uint32Array([1, 2, 3, 4]);
    let t09 = new Float32Array([1.5, 2.5, 3.5, 4.5]);
    let t10 = new Float64Array([1.5, 2.5, 3.5, 4.5]);
    
    let arrayBuffer = new ArrayBuffer(16);
    

    (TypeScript playground with the previous script, MDN documentation)

    您可以看到类型化数组在es6.d.ts 文件中:

    /**
      * A typed array of 8-bit integer values. The contents are initialized to 0. If the requested 
      * number of bytes could not be allocated an exception is raised.
      */
    interface Int8Array {
        /** 
          * Returns an array of key, value pairs for every entry in the array
          */
        entries(): IterableIterator<[number, number]>;
        /** 
          * Returns an list of keys in the array
          */
        keys(): IterableIterator<number>;
        /** 
          * Returns an list of values in the array
          */
        values(): IterableIterator<number>;
        [Symbol.iterator](): IterableIterator<number>;
    }
    

    这就是你所追求的吗?

    编辑:

    lib.es6.d.ts 将ArrayLike 定义为:

    interface ArrayLike<T> {
        length: number;
        [n: number]: T;
    }
    

    Iterable 一样

    interface Iterable<T> {
        [Symbol.iterator](): Iterator<T>;
    }
    

    这是一个与你的非常相似的函数的定义方式:

    interface Uint8ArrayConstructor {
    
        //
        // ...
        // 
    
        /**
          * Creates an array from an array-like or iterable object.
          * @param arrayLike An array-like or iterable object to convert to an array.
          * @param mapfn A mapping function to call on every element of the array.
          * @param thisArg Value of 'this' used to invoke the mapfn.
          */
        from(arrayLike: ArrayLike<number> | Iterable<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint8Array;
    }
    

    所以我将你的函数定义为:

    function foo(arrayLike: ArrayLike<number> | Iterable<number>) { ... }
    

    【讨论】:

    • 谢谢。我正在寻找编写一个函数来获取 TypedArray 或 Array 参数。例如:function foo(a:ArrayLike&lt;number&gt;) {...。看起来我应该使用Iterable&lt;number&gt;。但是使用 Atom,如果我将鼠标悬停在 new Float32Array(a) 上,它会说构造函数采用 ArrayLike&lt;number&gt;
    • 我编辑了我的答案。也许它可以帮助您了解更多。
    猜你喜欢
    • 2016-06-04
    • 1970-01-01
    • 2011-11-20
    • 2014-09-11
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    • 2015-02-20
    • 2023-03-11
    相关资源
    最近更新 更多