首页 > 解决方案 > 您如何理解 WebStorm 的功能接口?

问题描述

我无法理解某些功能及其接口。IE:

ReadonlyArray<unknown>.map( 
    callbackfn: (value: unknown, index: number, array: unknown[]) => unknown,
    thisArg?: any): unknown[]

.map()当我将鼠标悬停在一个函数上时,这就是 WebStorm 显示的内容。我知道.map()将指定的回调函数作为参数,但是=> unknown, 和thisArg?: any代表什么?

同样,当我将鼠标悬停在一个forEach()函数上时,弹出窗口如下所示:

ReadonlyArray<unknown>.forEach(
     callbackfn: (value: unknown, index: number, array: unknown[]) => void,
     thisArg?: any): void

forEach 也接受一个回调函数作为参数,但是=> void, thisArg?: any): void代表什么呢?

标签: javascriptwebstorm

解决方案


// the `.map` function is member of the generic type ReadonlyArray<unknown>. Where unknown is the type of the elements of the array
ReadonlyArray<unknown>.map( 
    // first argument is a callback function. The first argument of the callback is value, of type unknown, the second is index of type number, and so on
    callbackfn: (value: unknown, index: number, array: unknown[]) 
       // this parts means that the callback should return a value of type unknown
       => unknown,
    // .map takes a second arguments called thisArg of any type. The ? means that the argument is optional
    thisArg?: any)
    // .map returns an array of type unknown
    : unknown[]

该类型是未知的,因为它取决于所.map应用的数组中存在的元素的类型。如果是对象数组,则未知类型为object

同样,forEach接受第一个参数,它是一个回调。但是回调什么也不返回,因此你有=> void.


推荐阅读