Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8891565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:46:43+00:00 2026-06-14T22:46:43+00:00

I have the following code: module array { export function contains(arr: Array, item: any):

  • 0

I have the following code:

module array {
  export function contains(arr: Array, item: any): bool { // implementation }
}

What I want to be able to have is either union type ( in the example above Array, NodeList and IArguments should be possible) or multiple signatures for the exported function. I seem to not be able to do either.

I tried declaring the function multiple times with the different typing of the arguments, but the compiler complains for the lack of return statement – there is no point of proving implementation for a declaration used only for type checking of the arguments (and makes the code verbose). I think a much better thing would be to have union types, no?

So the question is: how do I achieve type check for functions where the allowed argument is of different type (in this case – array like object), but the implementation is the same, without having to copy/paste the implementation (basically a simple ‘return false’ probably will do in the declarations before the last one, but still seems like a bad idea).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:46:45+00:00Added an answer on June 14, 2026 at 10:46 pm

    You could use the any keyword to have a dynamic parameter:

    export function contains(arr: any[], item: any): bool { // implementation }
    

    Or if you know the types you want to allow, you can overload the function:

    export function contains(arr: string[], item: string): bool;
    export function contains(arr: number[], item: number): bool;
    export function contains(arr: any[], item: any): bool { // implementation }
    

    I’ve used string and number as an example – but your can use whatever types you like. I also assumed that if you have a list of strings, you’d be looking for a string item but you can change this if it isn’t the case.

    The final function isn’t callable – you can only access it via the overloads.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: class MyClass module MyModule class << self attr_accessor :first_name
I have the following code in my Mojolicious application module. When I run this
I have the following code in my simple ubercart payment gateway module to redirect
I have the following code in a custom module to save a session_id for
I have the following code in a kernel module that walks up the process
I have the following code: #include <iostream> #define BOOST_TEST_MODULE my test #include <boost/test/unit_test.hpp> BOOST_AUTO_TEST_CASE(
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig
I have following code <div id=main> <div id=one> </div> <div id=two> </div> <div id=three>
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have following code for loading image from url in xml parsing endElement method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.