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: 
From the [documentation](https://www.fastify.io/docs/latest/TypeScript/):

> 
> Using the two interfaces, define a new API route and pass them as generics. The shorthand route methods (i.e. .get) accept a generic object RouteGenericInterface containing five named properties: `Body`, `Querystring`, `Params`, `Headers` and `Reply`.
> 
> 
> 

You can use the `Reply` type.

```js
interface MiscIPAddressRes {
  ipv4: string
}
server.get<{
  Reply: MiscIPAddressRes
}>('/misc/ip-address', async (req, res) => {
  res
    .status(_200_OKAY)
    .send({
      ipv4: req.ip // this will be typechecked
  })
})

```
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
};

```