Question: I am having a Javascript snippet to store only unique items in a array of objects like this:-

```
const array = [
    {id: 3, name: 'Central Microscopy', fiscalYear: 2018},
    {id: 5, name: 'Crystallography Facility', fiscalYear: 2018},
    {id: 3, name: 'Central Microscopy', fiscalYear: 2017},
    {id: 5, name: 'Crystallography Facility', fiscalYear: 2017},
    {id: 3, name: 'Central Microscopy', fiscalYear: 2019},
  ];
  const result = [];
  const map = new Map();
  for (const item of array) {
    if (!map.has(item.id) ) {
      map.set(item.id, true); // set any value to Map
      result.push({
        id: item.id,
        name: item.name,
        fiscalYear: item.fiscalYear
      });
    }
  }
  console.log(result);

```

for this result is

```
[
  { id: 3, name: 'Central Microscopy', fiscalYear: 2018 },
  { id: 5, name: 'Crystallography Facility', fiscalYear: 2018 }
]

```

Now I want to have unique items and have those items whose fiscal years are greater than existing items.
For example:-
For the same input, the output should be as:- i need the code in javascript or typescript(Language constraint)

```
[
  { id: 3, name: 'Central Microscopy', fiscalYear: 2019},
  { id: 5, name: 'Crystallography Facility', fiscalYear: 2018 }
]

```

Need an optimal solution even if the data is large

Answer: 
After looking at the type definitions, I found out that there is also an alternative way to only type-check the handler (like in Julien TASSIN's answer), like this:

```
import { FastifyReply, FastifyRequest, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerDefault } from "fastify";
import { RouteGenericInterface } from "fastify/types/route";

interface IUser {
  firstname: string;
  lastname: string;
}

interface IUserRequest extends RouteGenericInterface {
  Params: { username: string };
  Reply: IUser; // put the response payload interface here
}

function getUserHandler(
  request: FastifyRequest<IUserRequest>,
  reply: FastifyReply<
    RawServerDefault,
    RawRequestDefaultExpression,
    RawReplyDefaultExpression,
    IUserRequest // put the request interface here
  >
) {
  const { username } = request.params;

  // do something

  // the send() parameter is now type-checked
  return reply.send({
    firstname: "James",
    lastname: "Bond",
  });
}

```

You can also create your own interface with generic to save writing repeating lines, like this:

```
import { FastifyReply, FastifyRequest, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerDefault } from "fastify";
import { RouteGenericInterface } from "fastify/types/route";

export interface FastifyReplyWithPayload<Payload extends RouteGenericInterface>
  extends FastifyReply<
    RawServerDefault,
    RawRequestDefaultExpression,
    RawReplyDefaultExpression,
    Payload
  > {}

```

then use the interface like this:

```
function getUserHandler(
  request: FastifyRequest<IUserRequest>,
  reply: FastifyReplyWithPayload<IUserRequest>
) {
  const { username } = request.params;

  // do something

  // the send() parameter is also type-checked like above
  return reply.send({
    firstname: "James",
    lastname: "Bond",
  });
}

```
If you want to type the handler only, you can perform it this way

```
import { RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerDefault, RouteHandler, RouteHandlerMethod } from "fastify";

const getUserHandler: RouteHandlerMethod<
    RawServerDefault,
    RawRequestDefaultExpression,
    RawReplyDefaultExpression,
    { Reply: IUser; Params: { username: string } }
> = async (
  req: FastifyRequest<IUserRequest, RawServerBase, IncomingMessage | Http2ServerRequest>
) => {
  const { username } = req.params;
  return { ... }; // Would like to return instance of IUser
};

```