1

When I compile the following code (which to me seems incorrect because the type of the const and the type of the function () are different) no errors are produced:

export const yearsExtractor: (Period) => Year[] = (periods: Period[]): Year[] => periods && periods.length ? periods.map((period: Period) => period.year) : <Year[]>[];

When I compile the following code (which to me seems correct because the type of the const and the type of the function () match) an error is produced:

export const yearsExtractor: (Period[]) => Year[] = (periods: Period[]): Year[] => periods && periods.length ? periods.map((period: Period) => period.year) : <Year[]>[];

The difference being that the code that is not compiling is declaring the const as a function that accepts an array of Period objects (as opposed to a single Period object).

error

(Period[]) =>

no error

(Period) =>

1
  • 1
    You always have a mandatory name before the optimal type annotation. Therefore, Period is a parameter name at that position and the [] is invalid at that position... Commented Feb 14, 2017 at 1:19

1 Answer 1

3

in the first instance:

(Period) => Year[] 

reads as a function, with parameter Period:any, the second instance:

(Period[]) => Year[]... 

is invalid syntax because you've not given a name to the function variable (you need to).

try (period: Period[]) => Year[]...

export const yearsExtractor: (period: Period[]) => Year[] = (periods: Period[]): Year[] => periods && periods.length ? periods.map((period: Period) => period.year) : <Year[]>[];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.