如何指定Typescript函数参数数组类型

如何指定Typescript函数参数数组类型,typescript,Typescript,当我编译以下代码时(在我看来这似乎是不正确的,因为常量的类型与()函数的类型不同),不会产生错误: export const yearsExtractor: (Period) => Year[] = (periods: Period[]): Year[] => periods && periods.length ? periods.map((period: Period) => period.year) : <Year[]>[]; export c

当我编译以下代码时(在我看来这似乎是不正确的,因为
常量的类型与
()
函数的类型不同),不会产生错误:

export const yearsExtractor: (Period) => Year[] = (periods: Period[]): Year[] => periods && periods.length ? periods.map((period: Period) => period.year) : <Year[]>[];
export const yearsExtractor: (Period[]) => Year[] = (periods: Period[]): Year[] => periods && periods.length ? periods.map((period: Period) => period.year) : <Year[]>[];
不同之处在于,未编译的代码将
const
声明为接受
Period
对象数组的函数(与单个
Period
对象相反)

错误

(句点[])=>

无错误


(Period)=>
首先:

(Period) => Year[] 
(Period[]) => Year[]... 
作为函数读取,带有参数
Period:any
,第二个实例:

(Period) => Year[] 
(Period[]) => Year[]... 
无效语法,因为您尚未为函数变量指定名称(需要)

请尝试
(期间:期间[])=>年份[…

export const yearsExtractor: (period: Period[]) => Year[] = (periods: Period[]): Year[] => periods && periods.length ? periods.map((period: Period) => period.year) : <Year[]>[];
export const yearsExtractor:(期间:期间[])=>Year[]=(期间:期间[]):Year[]=>期间和期间。长度?periods.map((period:period)=>period.year):[];

首先:

(Period) => Year[] 
(Period[]) => Year[]... 
作为函数读取,带有参数
Period:any
,第二个实例:

(Period) => Year[] 
(Period[]) => Year[]... 
无效语法,因为您尚未为函数变量指定名称(需要)

请尝试
(期间:期间[])=>年份[…

export const yearsExtractor: (period: Period[]) => Year[] = (periods: Period[]): Year[] => periods && periods.length ? periods.map((period: Period) => period.year) : <Year[]>[];
export const yearsExtractor:(期间:期间[])=>Year[]=(期间:期间[]):Year[]=>期间和期间。长度?periods.map((period:period)=>period.year):[];

在最佳类型注释之前,始终有一个强制名称。因此,句点是该位置的参数名称,[]在该位置无效……在最佳类型注释之前,始终有一个强制名称。因此,句点是该位置的参数名,[]在该位置无效。。。