0
当我编译下面的代码(因为const
的类型和功能()
的类型是不同的这对我来说似乎是不正确的)没有错误的产生:如何指定打字稿函数参数数组类型
export const yearsExtractor: (Period) => Year[] = (periods: Period[]): Year[] => periods && periods.length ? periods.map((period: Period) => period.year) : <Year[]>[];
当我编译下面的代码产生一个错误(因为const
的类型和功能()
匹配的类型这对我似乎是正确的):
export const yearsExtractor: (Period[]) => Year[] = (periods: Period[]): Year[] => periods && periods.length ? periods.map((period: Period) => period.year) : <Year[]>[];
的区别在于不编译的代码将const
声明为接受Period
对象数组(而不是单个Period
对象)的函数。
错误
(Period[]) =>
在一审中没有错误
(Period) =>
你总是有最佳的类型标注前一个强制性的名字。因此,Period是该位置的参数名称,[]在该位置无效...... – Lucero