---
title: "Discussion on \"The right way to create utility functions in TypeScript\" | Hashnode"
description: "Discussion on \"The right way to create utility functions in TypeScript\". Utility functions are part of every project.\nYou might organize them in folders called helper, lib, or shared, but their function is the same:\nThey make sure your business logic stays consistent in your game.\n(Drop a like if you love the rhyme. 🤫🎤�..."
url: "https://hashnode.com/posts/the-right-way-to-create-utility-functions-in-typescript/659812adc22d321fff4af818"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [The right way to create utility functions in TypeScript](https://akoskm.com/the-right-way-to-create-utility-functions-in-typescript?utm_source=hashnode&utm_medium=feed)

Utility functions are part of every project. You might organize them in folders called helper, lib, or shared, but their function is the same: They make sure your business logic stays consistent in your game. (Drop a like if you love the rhyme. 🤫🎤�...

Jan 5, 2024 · 3 min read · akoskm.com  
Read the article

-   [#typescript](https://hashnode.com/tag/typescript)
-   [#web-development](https://hashnode.com/tag/web-development)
-   [#beginners](https://hashnode.com/tag/beginners)
-   [#design-and-architecture](https://hashnode.com/tag/design-and-architecture)
-   [#javascript](https://hashnode.com/tag/javascript)

## Written by

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

@akoskm

  
[View profile](https://hashnode.com/@akoskm)

Full-Stack Developer and Writer

I build web apps and make educational content to help web developers.

## Responses 3

Markdown supported · ⌘↵ to post

[Gabby Katherine](https://hashnode.com/@GabbyKather)

[Aug 17, 2024](https://hashnode.com/posts/the-right-way-to-create-utility-functions-in-typescript/659812adc22d321fff4af818/comment/66c0f5a1a3db1be9b505d36b)

In the ever-evolving landscape of the digital currency market, the recent resurgence of Bitcoin has captivated the attention of investors and enthusiasts alike. At the forefront of this remarkable comeback is the skilled team at Cyberspacehackpro, whose exceptional expertise in cryptocurrency has enabled them to navigate the complexities of the Bitcoin market with unparalleled precision. Through their strategic interventions and innovative techniques, Cyberspacehackpro has not only helped Bitcoin regain its footing but has also played a pivotal role in rescuing the personal holdings of countless individuals, including the impressive sum of 43,000 units that were previously thought to be lost. This remarkable feat showcases the depth of Cyberspacehackpro's technical prowess and their unwavering commitment to empowering individuals in the digital currency realm. By leveraging their extensive knowledge of blockchain technology and their mastery of advanced decryption methods, the team at Cyberspacehackpro has been able to navigate the intricate web of cryptocurrency transactions, identifying and recovering funds that had eluded their owners for an extended period. This remarkable accomplishment has not only restored the financial security of those affected but has also bolstered the confidence of the broader Bitcoin community, demonstrating the transformative power of specialized expertise in the face of seemingly insurmountable challenges. As the cryptocurrency market continues to evolve and present new opportunities and obstacles, the success of Cyberspacehackpro in the comeback of Bitcoin serves as a testament to the value of strategic thinking, technical prowess, and a relentless pursuit of solutions. This impressive achievement underscores the vital role that specialized service providers like Cyberspacehackpro play in empowering individuals and institutions to navigate the dynamic and often unpredictable world of digital currencies, ultimately paving the way for a more secure and prosperous financial future. Call:

WhatsApp: +1 631 428-0567

[Lilian Hunter](https://hashnode.com/@Lilianhunter)

[Aug 17, 2024](https://hashnode.com/posts/the-right-way-to-create-utility-functions-in-typescript/659812adc22d321fff4af818/comment/66bfedf25852b482995623d9)

SPEAK WITH A REASONABLE, LICENSED BTC/USDT RECOVERY HACKER - ALPHA KEY

After being unable to withdraw or send money from my block chain wallet address, I became a victim of a crypto investment scam. I found online reviews of Alpha Key Recovery's hacker expert services and how they have been assisting victims of scams involving cryptos. If you have fallen victim to any kind of scam, please contact them using the information below. They continue to be the most dependable crypto hacker that I can recommend to anyone.

WhatsApp Messenger: +15714122170 Signal Message: +18622823879 Alpha Key Recovery Telegram channel

[Jackson David](https://hashnode.com/@jackson943)

[Aug 13, 2024](https://hashnode.com/posts/the-right-way-to-create-utility-functions-in-typescript/659812adc22d321fff4af818/comment/66baee07bc530e1bd4143ce9)

i live in Canada a friend introduces me to blank atm card which i got from Davidwilson hackers, i believe him so well not like scammers out there i have withdrew $50000 with this card so i decided to share a testimony if you are interested contact him now for Financial Solutions Davidwilson2284@gmail.com

[Star lights](https://hashnode.com/@starlightfillers)

[Aug 9, 2024](https://hashnode.com/posts/the-right-way-to-create-utility-functions-in-typescript/659812adc22d321fff4af818/comment/66b64c33a5a2b561e5d516a2)

i really need to know why

[Tải Go88](https://hashnode.com/@taigo88lat24)

[Aug 8, 2024](https://hashnode.com/posts/the-right-way-to-create-utility-functions-in-typescript/659812adc22d321fff4af818/comment/66b4362d77ef9e88dc2b6942)

Câu hỏi thường gặp là một trong những lý do chính thu hút người chơi đến với nhà cái này. Những khuyến mãi này giúp anh tăng vốn và mang lại nhiều lợi ích khác trong quá trình chơi game tại nhà cái. Hãy cùng Rikvip tìm hiểu trong bài viết dưới đây.

Xem thêm : [xtremevn.com/cau-hoi-thuong-gap](https://xtremevn.com/cau-hoi-thuong-gap/)

Địa chỉ: 84/6 Đ. Tôn Thất Hiệp, Phường 13, Quận 11, Thành phố Hồ Chí Minh, Việt Nam

Email: taigo88lat@gmail.com

Số điện thoại: 038 273 4012

#go88

#taigo88

#tải\_go88

#cachtaigo88

#codego88

#linktaigo88

#taigamego88

[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).

[Md Taqui imam](https://hashnode.com/@taqui786)

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

🔥🔥🔥
