How do I determine if a variable with multiple types as its return type is of type string[]?

let RowData: PricingOrder[] | Notification[] | ActionLog[] | string[];

Assuming that RowData can also be of type string[] (

For example, RowData = ['apple','banana','cherry'];
)

if(typeof RowData === 'string[]'){
  //perform operations specific to string arrays
} else {
  //perform operations specific to custom class arrays
}

Answer №1

It seems like your comment suggests that your array is not of a combinational type. In order to determine this, you just need to confirm two things:

  1. Firstly, is your result an array?
  2. Secondly, what is the data type of the first value in your array?

var srray = ["a","b"];

let isArrayStringType = false;

if(Array.isArray(srray)){
   isArrayStringType = srray.length > 0 && typeof srray[0] == 'string'
}

console.log(isArrayStringType)

Update

The return type of your function does not include the string type, so you can easily verify this by checking the second one: typeof srray[0] == 'string'

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

Guide to creating an Image slider using javascript/jQuery with setInterval animation

I'm looking to add a slide effect to the sudden image changes happening. Here's the code I have: HTML <div><img src="1.jpg" id="sliderImage"></div> This is the script I'm using: var myImage = document.getElementById("sl ...

Javascript menu toggle malfunctioning with sub-menus

I am in the process of creating a responsive menu for a complex website. The horizontal menu transitions into a vertical layout on smaller screens, with javascript used to toggle the sub-menu items open and closed when clicked. One issue I am facing is wit ...

Setting up Karma to import global scss files in an Angular-CLI project: a simple guide

I am struggling to configure angular-cli + scss + karma for testing components together. When I run ng test, the karma unit tests only include the components' own scss styles. To incorporate my main styles.scss in the tests, I attempted to adjust the ...

Comparing Dates in Node.js using JavaScript and Sorting Arrays based on Date Values

Is JavaScript lacking a basic Date comparison feature similar to Python? Within my Node.js script, these lines are present: console.log(Date(2012,11,10) < Date(2012, 11, 9)) console.log(Date(2012,11,10) > Date(2012, 11, 9)) Surprisingly, both of t ...

The value of the progress bar in Bootstrap Vue cannot be retrieved using a function

I have implemented a progress bar using Bootstrap Vue. Here is the HTML code: <b-progress :value="getOverallScore" :max=5 variant="primary" animated></b-progress> The getOverallScore function calculates an average value based on three differe ...

Plain objects are necessary for actions. In cases where thunk is already present, consider using custom middleware for handling async actions

Despite implementing thunk middleware, I continue to encounter impure errors. Any assistance would be highly appreciated. The section where I invoke the creator: import React from 'react' import { StyleSheet, Text, View, Button } from 'rea ...

Enable the button when there is an error after submission in AngularJS

Has anyone encountered issues with dynamically disabling a button? I noticed that my API delays for 2 seconds to mimic a slow connection. I expected the submit button to be disabled upon submission and then re-enable itself. In HTML, manually disabling t ...

The JWT authentication token is not valid for routes outside of the /api endpoint

Encountering an issue with JWT authentication token while using Laravel's broadcasting feature. The error message received is: {error: "token_invalid"} Puzzlingly, the token appears to be valid as it works perfectly fine on /api routes but fails on n ...

Error in NodeJs: ReferenceError - the variable I created is not defined

Encountering an issue while attempting to use a module in my router file. I have successfully required it, but now I am seeing the following error message: ReferenceError: USERS is not defined at c:\work\nodejs\router\main.js:32 ...

Comparing the Features of Angular 7 and Angular 8

Could someone explain the key distinctions between Angular 7 and Angular 8, especially focusing on the significant new addition of the IVY engine? I would appreciate details or a link to delve deeper into this concept for better understanding. ...

Communicating Data Between Controllers in Node.js

Within my PlantsController, I extract the Plants using the following method: function getAll() { const plants= HttpRequestPromise.get(config.ApiURL+'allPlants', config.head); return plants; } export class PlantsController { ... public ...

A guide on setting the value for 'data-target' in Angular 5

I have a button with a data-target attribute. I am looking to make the data-target attribute's value conditionally change based on a Boolean condition. HTML CODE: <button class='pricing-addon' style="margin-left: 20px" data-toggle="mod ...

Implementing dynamic AngularJS functionality within an older HTML structure using JQuery

I am facing an issue while trying to load dynamic content using AngularJS. In my system, I have legacy pages that utilize JQuery for dynamic loading. However, as I am developing new features with AngularJS, I am encountering the same problem with dynamic ...

Clicking will close any other sub-menus

Greetings everyone, Thank you for taking the time to read through this post. I am currently facing an issue while attempting to create a menu with sub-menus. Here are the requirements for the menu: Sub-menus should open on click. (working) Selected sub ...

Converting a multidimensional JSON object to HTML using jQuery's getJSON function

I've been struggling with a task and can't seem to figure it out. I need to insert a form into an HTML file using data from a JSON file, but the catch is that I can only write in a .js file (using jQuery) and not directly in the json or html file ...

Using a Default Value in a Destructured Function Parameter Results in a ReferenceError

When working on setting a default value for the db in my CRUD functions for testing purposes, I encountered a peculiar issue that has left me puzzled. Here's the snippet of code that caused the trouble: import { db } from './firebase' func ...

LernaJS TypeScript encounters difficulty locating module

I'm facing some challenges while attempting to integrate LernaJS with TypeScript. Specifically, when I try to execute my package-1 which relies on package-2 as a dependency, I encounter the following error: module.js:549 throw err; ...

What is the best way to generate a div with a dynamically changing variable as its ID?

Creating a quiz where the user can choose how many questions to answer. A function is used to generate individual question divs based on the user's input. Each question displays a Chinese character, and the user must select the correct translation. ...

Dependency management in ReactJS components

I am grappling with the optimal structure for a React component that is composed of other components. Let's look at the first example: <ColorSelect id="color" label={this.state.selectLabel} trigger={{ color: "lime", text: "Lime"}} onPropagateCli ...

Turning off the MaskedEditValidator and MaskedEditExtender

I have created a compact table that allows users to edit, delete, and add records: The table (DataGridView, bound) is user-friendly: click on "Edit" to enter the editing mode (which includes a "Cancel" option), update values, and then click "Add" to inser ...