美文网首页
Error: T doesn't have .length

Error: T doesn't have .length

作者: SailingBytes | 来源:发表于2019-08-01 14:39 被阅读0次

使用 TS 出现这个问题时,编译器提示错误,代表我们正在使用它的.length,但我们未提供.length这个数据类型。

// error

function collectionProps<T>(arg:  T):  T {

    console.log(arg.length);  // Error: T doesn't have .length    

    return arg;

}

// success 1

function collectionProps<T>(arg:  T[]):  T[] { 

    console.log(arg.length);

    return arg;

}

// success 2

function collectionProps<T>(arg:  Array<T>):  Array<T> { 

    console.log(arg.length);

    return arg;

}

相关文章

网友评论

      本文标题:Error: T doesn't have .length

      本文链接:https://www.haomeiwen.com/subject/ftwqdctx.html