---
title: "Comment by Furkan Emin Can on \"The right way to create utility functions in TypeScript\" | Hashnode"
description: "Thanks for the great article Ákos Kőműves . I&#39;ve played with your example and converted it to a type predicate function. The isCollaboratorExternal function"
url: "https://hashnode.com/posts/the-right-way-to-create-utility-functions-in-typescript/659812adc22d321fff4af818/comment/65998e705a246ab4fe8981ea"
---

[Discussion](https://hashnode.com/posts/the-right-way-to-create-utility-functions-in-typescript/659812adc22d321fff4af818)

Comment [The right way to create utility functions in TypeScript](https://hashnode.com/posts/the-right-way-to-create-utility-functions-in-typescript/659812adc22d321fff4af818)

[Furkan Emin Can](https://hashnode.com/@femincan)

[Jan 6, 2024](https://hashnode.com/posts/the-right-way-to-create-utility-functions-in-typescript/659812adc22d321fff4af818/comment/65998e705a246ab4fe8981ea)

Thanks for the great article [Ákos Kőműves](https://hashnode.com/@akoskm).

I've played with your example and converted it to a type predicate function.

The `isCollaboratorExternal` function accepts a `collaborator` parameter with type `string` and uses the `Array.some` function (because of [this issue](https://github.com/Microsoft/TypeScript/issues/26255)) to check if `externalCollaboratorTypes` has the `collaboratorType` and returns the type predicate.

Thus, after the check operation the type of `data.collaborator.type` will be `"External Editor" | "External Administrator"`.

I think types are more aligned with the logic in this solution.

Here is the link for TypeScript Playground: [TS Playground](https://www.typescriptlang.org/play?#code/MYewdgzgLgBAJgQygmBeGBvAUDGoA2+CARiAE5LkBcmOuMUAngA4CmNA5AOoCWAXgjJwOdAL5ZRAbixZQkWAGEA8gBkVAQQBCSgErqAKroD6+gJoAFAKIBlNLVwAiSwBEAkoZ0OaTgB5RWZGAI+DCWcDxQ5A4ANHQO6s4Asq4Acq7W+noeXjC+-oHBMOpwALY8YDzQFJFkMXEA4gCqNvo5DvUArqzQdY5K+gASljpGympauga6bUpQABYBMAoghCTklLUSMAgQeODQ0rL7sKx+AUH4y6uk1eT6LN12ANp0YxraWcZmVtYAdC7uXSxXBvCafEbfGy-BLJNIZcGxAC6hwAZh0wMAoDxwDBKlciDcNpYzgV8AAKAgE9Y1e5sGhVcoAcwAlDRKWtbmRaaxcbsmGwQCiYKd8hd8RyNtyIE8wB0SsQAoj7DAyKwoB1AsKSWKVlTOVLfhAQCVWGSRedguLCTSHmgAHxa0WW3USm1sNCodDs613B7M6TiI7yPbXam+93oRDIX7esNkX781jSAD0yfoMAAegB+GQ8IVkvEun1kYlO8lRhAxotxhN+5nKuTQEN6yW2yNISuxzm1th0VPp7MSIA)

[Ákos Kőműves](https://hashnode.com/@akoskm)

[Jan 6, 2024](https://hashnode.com/posts/the-right-way-to-create-utility-functions-in-typescript/659812adc22d321fff4af818/comment/65998e705a246ab4fe8981ea#reply-6599b86d5a246ab4fe8b00da)

This is interesting. Thanks for sharing it! What would be the advantage of using this over includes and returning a boolean?

[Furkan Emin Can](https://hashnode.com/@femincan)

[Jan 7, 2024](https://hashnode.com/posts/the-right-way-to-create-utility-functions-in-typescript/659812adc22d321fff4af818/comment/65998e705a246ab4fe8981ea#reply-659a541a5a246ab4fe8ed97a)

For the first part of your question, the reason why I didn't use `Array.includes` is because of a problem with TypeScript.

As far as I understand, TypeScript doesn't allow us to use a more generic typed search parameter for a more explicitly typed array in the `Array.includes`. So we can't use the type `string` with an array typed `("External Editor" | "External Administrator")[]`.

As a workaround we can set the parameter type `as any`, but I thought `Array.some` is more straightforward to work with (we don't need to disable type checking), and also I couldn't find any performance difference between the two.

For the second part of the question, returning a `boolean` doesn't give us any type narrowing.

If you use the function with an if statement, inside the statement, the type of `data.collaborator.type` is still `string`.

On the contrary, returning type predicate gives us type narrowing.

If we call this function with an `if` statement, inside the statement, the type of `data.collaborator.type` is narrowed to `"External Editor" | "External Administrator"`.

There is a visualization in the [TS Playground](https://www.typescriptlang.org/play?#code/MYewdgzgLgBAJgQygmBeGBvAUDGoA2+CARiAE5LkBcmOuMUAngA4CmNA5AOoCWAXgjJwOdAL5ZRAbixZQkWAGEA8gBkVAQQBCSgErqAKroD6+gJoAFAKIBlNLVwAiSwBEAkoZ0OaTgB5RWZGAI+DCWcDxQ5A4ANHQO6s4Asq4Acq7W+noeXjC+-oHBMOpwALY8YDzQFJFkMXEA4gCqNvo5DvUArqzQdY5K+gASljpGympauga6bUpQABYBMAoghCTklLUSMAgQeODQ0rL7sKx+AUH4y6uk1eT6LN12ANp0YxraWcZmVtYAdC7uXSxXBvCafEbfGy-BLJNIZcGxAC6hwAZh0wMAoDxwDBKlciDcNpYzgV8AAKAgE9Y1e5sGhVcoAcwAlDRKWtbmRaaxcbsmGwQCiYKd8hd8RyNtyIE8wB0SsQAoj7DAyKwoB1AsKSWKVlTOVLfhAQCVWGSRedguLCTSHmgAHxa0WW3USm1sNCodDs613B7M6TiI7yPbXam+93oRDIX7esNkX781jSAD0yfoMAAegB+GQ8IVkvEun1kYlO8lRhAxotxhN+5nKuTQEN6yW2yNISuxzm1th0VPp7MSIA) that shows the result of narrowing the type.

As I said, I think this approach is more suitable for the TypeScript side, for the JavaScript side there is nearly no difference.

[Ákos Kőműves](https://hashnode.com/@akoskm)

[Jan 8, 2024](https://hashnode.com/posts/the-right-way-to-create-utility-functions-in-typescript/659812adc22d321fff4af818/comment/65998e705a246ab4fe8981ea#reply-659bc69eb80581bc1952d343)

Thanks for the explanation [Furkan Emin Can](https://hashnode.com/@femincan).

I looked at your example once more, and I think it does more (which completely justifies the different typing) compared to my simple example. Yours returns the actual collaborator type if the supplied `data.collaborator.type` matches a collaborator type.

In my example, I simply want to know if the value of `data.collaborator.type` is an external collaborator. Since it's either yes or no, `boolean` gives sufficient type narrowing (true/false).
