The type 'true | CallableFunction' does not have any callable constituents

The error message in full:

This expression is not callable.
  No constituent of type 'true | CallableFunction' is callable

Here is the portion of code causing the error:

public static base(
    text,
    callB: boolean | CallableFunction = false,
    
  ) {
    const sw = Swal.fire({
      text,
      });

    if (callB) {
      sw.then(callB());
    }
  }

To address the issue, I updated the type of callB to :

 callB: (param: any) => void |boolean = false

However, upon removing the callB type definition:

callB= false

A new error emerged:

This expression is not callable.
  Type 'Boolean' has no call signatures

Answer №1

Here is the solution you've been seeking:

function base(text: string, callB: false | CallableFunction = false) {
  console.log(text);
  if (callB) {
    callB();
  }
}

The main idea is to pass a function or false. If the value is truthy, the function should be called.

However, based on your specifications

function base(text: string, callB: boolean | CallableFunction = false) {
  console.log(text);
  if (callB) {
    callB();
  }
}

This sample call would work: base('some str', true) where callB is true and not a function that will be executed using callB(). A non-callable function cannot be executed, right? Hence the error.

Check out the TypeScript Playground link for more details: https://tsplay.dev/m056qW

Answer №2

When dealing with the type

boolean | Function

it essentially translates to

true | false | Function

Since a value of type Function is considered truthy, the following check

if (callB)

limits the type of callB to

true | Function

and considering that true cannot be called, it results in an error.

If you are unsure about how to utilize your base method, here are two potential solutions:

If accepting true is not appropriate, modify your parameter type accordingly

static base(
    text, 
    callB: false | CallableFunction = false
) {
    if (callB) {
        callB();
    }
}

If handling true is necessary, employ a more precise conditional statement like

if (typeof callB === 'function') {
    callB() // It will work fine
}

Answer №3

When utilizing the combination of false | Callablefunction (or boolean | CallableFunction), it is possible to force a type conversion by employing the typeof operator:

static base(
    text,
    callB: false | CallableFunction = false
) {
    if (typeof callB === "function") {
        callB()
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to access local JSON file data in React.js

I have been struggling to retrieve data from a JSON file located within my ReactJS project directory. Even attempting to access the package.json file within the ReactJS folder resulted in errors. How can I successfully extract data from a local JSON file ...

Stylish Navigation Menu Logo/Site Name

Currently in the process of upgrading my website and I have my website name included as part of my menu, as opposed to a logo. This has been an easy solution that has worked well for me. For mobile screens, the template I purchased includes a slicknav men ...

Creating an endless scrolling feature with Ionic 3

My tech stack includes symfony3 and FosRestBundle for the backend, and Ionic 3 for the frontend development. While attempting to implement an InfiniteScroll feature following the Ionic documentation, I encountered an issue where only the loading text and ...

The service being injected is not defined

Two services are involved in this scenario, with the first service being injected into the second service like so: rule.service.ts @Injectable() export class RuleService { constructor( private _resourceService: ResourceService ){} s ...

Ways to restart script following Ajax call when additional search results are loaded

Implementing Klevu's search results page has been a manageable task so far. However, I encountered an issue where the search results page is displaying an Add to Cart button that should not be there, as confirmed by Klevu themselves. Their suggestion ...

What is the best way to change a Buffer array into hexadecimal format?

After making a call to one of my API endpoints, I am receiving a Buffer array in a JSON object. My goal is to convert this array into a more user-friendly format such as hex so that I can easily compare them. Below is a snippet of the current object struct ...

Tips for including HTML tags within a string using node.js, express, and ejs

Currently, I am utilizing Node.js with Express and EJS for my project. My goal is to pass HTML tags within a string to the browser in the following manner: listRequests.forEach(function(key) { messages.push("You have a message from <b>" + key.us ...

Perform an action upon a successful completion of an AJAX request using Axios by utilizing the `then()` method for chaining

I'd like to trigger a specific action when an ajax call is successful in axios save() { this.isUpdateTask ? this.updateProduct() : this.storeProduct() this.endTask() } When the ajax call to update or store the product succeed ...

The style of MUI Cards is not displaying properly

I've imported the Card component from MUI, but it seems to lack any styling. import * as React from "react"; import Box from "@mui/material/Box"; import Card from "@mui/material/Card"; import CardActions from "@mui/m ...

NGRX 8 reducer now outputting an Object rather than an Array

I am facing an issue where the data returned from the reducer is an object instead of an array. Despite trying to return action.recentSearches, it doesn't seem to work as expected. The data being returned looks like this: { "loading": false, "recent ...

What is the best way to save a webpage when a user clicks a button before leaving the page with Javascript

I've exhausted all my options in trying to find a way to trigger a button that saves the page upon exit, but it never seems to work. I need the event to occur even if the button is not visible at the time of the page exit. The new security protocols o ...

A Great Option for Easy Drag and Drop Functionality

Currently researching options for Drag and Drop functionality with items such as images and textareas. Need a solution that provides coordinates of where the item is dropped on screen, saves it, and allows placement in the exact same location later. Desir ...

Navigating to specific rows in a primeng virtualscroll table using the scrollToIndex

Utilizing Primeng virtual scroll table in Angular to manage large datasets in an array. I am interested in using the scrollToIndex. Is there an equivalent of cdk-virtual-scroll-viewport in Primeng table? I need the functionality where, upon a user clicking ...

A guide on sorting an array based on elements from a different array

We are currently in the process of developing an application using Vue and Vuex. Our goal is to display a list of titles for venues that a user is following, based on an array of venue IDs. For instance: venues: [ {venue:1, title: Shoreline} {venue:2, ti ...

Experiencing excessive memory usage when attempting to load a large JSON file in Firefox

We are in the process of developing a client-based application using HTML5 and indexedDB on Firefox 28. When large amounts of data are loaded to Firefox for the first time using AJAX requests in JSON format, each JSON response is approximately 2MB (gzipped ...

Is there a way to simulate Pinia and vue-i18n simultaneously?

Exploring Vue 3's Composition API with a twist: The store.ts file import { ref, Ref } from 'vue'; import { defineStore } from 'pinia'; export const useStore = defineStore('store', () => { const isLoading: Ref<bo ...

Is it possible to encounter a MongoDB error for the $or operator in a correctly formatted query?

Here is the problem I am facing: const users = this.mongo.db.collection('Users') let query = { '$or': [ { "email": {'$eq': req.body.email }}, {"username": {'$eq': req.body.username }} ] } users.fi ...

Obtain the specific key's value from a new Map state

In my code, I've defined a variable called checkedItems as a new Map(); When I try to console.log(checkedItem), the output is as follows: Map(3) {"1" => true, "1.5" => true, "2" => false} Is there a way ...

The CustomValidator ClientValidationFunction will only activate on the second or subsequent submission if CheckBoxList is checked

I am encountering an issue with a user control that is dynamically added to pages on DNN. The user control is built on a customized version of CheckBoxList and includes a CustomValidator with ClientValidationFunction set to a javascript function. It works ...

AngularJS input range is written inside the bubble that crosses over the screen

Is there a way to write inside the bubble of the range slider? I have adjusted the design of the range slider and now I simply want to display the number inside the bubble: Please see image for reference I aim to display the number inside a circle. What ...