TypeScript:ArrayLike接口缺少索引签名

时间:2016-07-11 15:29:15

标签: typescript

我尝试使用TypeScript内置接口<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="somethingextra"> <xsl:copy> <!-- here you switch the mode --> <xsl:apply-templates select="*" mode="noTuple"/> </xsl:copy> </xsl:template> <!-- in standard mode - copy the node an go on in standard mode --> <xsl:template match="*"> <xsl:copy> <xsl:apply-templates select="*|text()"/> </xsl:copy> </xsl:template> <xsl:template match="text()"> <xsl:value-of select="."/> </xsl:template> <!-- matches only for tuple nodes in extra mode --> <xsl:template match="tuple" mode="noTuple"> <!-- back to normal mode --> <xsl:apply-templates select="*|text()"/> </xsl:template> <!-- in extra mode - copy the node an go on in extra mode --> <xsl:template match="*" mode="noTuple"> <xsl:copy> <xsl:apply-templates select="*|text()" mode="noTuple"/> </xsl:copy> </xsl:template> <xsl:template match="text()" mode="noTuple"> <xsl:value-of select="."/> </xsl:template> </xsl:stylesheet> ,但仍然收到如下错误消息:

  

错误:(12,11)TS2420:Class&#39; Point&#39;错误地实现了界面&#39; ArrayLike&#39;。类型&#39;点&#39;

中缺少索引签名
ArrayLike

我该如何解决这个问题? (或任何解决方法?)

2 个答案:

答案 0 :(得分:1)

如何实现界面ArrayLike

class Point implements ArrayLike<number> {
    [n: number]: number;
    length: number = 1;
    [0]: number = 10;
}

let p = new Point()
p[23] = 34; // ok
p[23] = "34"; // Error: Type 'string' is not assignable to type 'number'

答案 1 :(得分:0)

如果您希望Point成为数组,那么为什么不只是扩展Array类?

class Point extends Array<number> {
    // add Point array methods here
}

这样Point会自动实现ArrayLike<number>界面。