Encountering the error message "Cannot assign 'Void' to boolean while employing the .find() function"

One particular line of code is causing the issue at hand.

const t: GridType   = gridDef.find( a => { a.GridName == core.GridStyle; return a; } );

The error message that I am encountering reads as follows

ERROR in src/app/grid-builder/builder-scratch.ts(255,43): error TS2345: Argument of type '(this: void, a: GridType) => void' is not assignable to parameter of t ype '(this: void, value: GridType, index: number, obj: GridType[]) => boolean'. Type 'void' is not assignable to type 'boolean'.

The objective behind setting up a class to extend to Angular Components for the purpose of dynamically creating grids is as follows.

export class GridBuilder{
    Settings : GridInit;
    GridData : GridType[];
    Grid     : string    = '';


    constructor() {
        this.Settings.GridStyle = 'SiteGridA';
        this.GridData = GridDefs;
        this.buildGrid( this.Settings, this.GridData );
    }

    buildGrid( core: GridInit, gridData: GridType[] ) {
        const w: number     = multiply( core.Size.Width, core.Size.PixelRatio );
        const h: number     = multiply( core.Size.Height, core.Size.PixelRatio );
        const o: string     = checkOrientation( w, h );
        const c: CellSpecs  = calcCell( o, w );
        const t: GridType   = gridData.find( a => { a.GridName == core.GridStyle; return a; } );

        const cols: string  = calcArea( t.Columns, c );
        const rows: string  = calcArea( t.Rows, c );

        this.Grid = cols + '/' + rows;
    }
}

The array GridType[] mentioned in the code snippet above is structured in the following manner

export interface GridType {
    GridName : string;
    Columns  : GridLine[];
    Rows     : GridLine[];
}

//other interfaces chaining into it
export interface GridLine {
    Names    : LineName[];
    Type     : string;
    CalcType : string;
    CSize    : CellSize;
}

export interface LineName { Name: string; }

export interface CellSize {
    Size?   : number;
    Repeat? : number;
    Min?    : number;
    Max?    : number;
}

Returning to the constructor

constructor() {
    this.Settings.GridStyle = 'SiteGridA';
    this.GridData = GridDefs;
    this.buildGrid( this.Settings, this.GridData );
}

The intentions are

  1. Set the parameter to SiteGridA to be matched within the .find() method to retrieve grid data.
  2. Save the grid data to a variable that will be used by the .find() method.
  3. Pass variables into the function to construct the grid.

The GridDefs constitutes the grid definitions structured as shown below

    export const GridDefs: GridType[] = [
    {
        GridName : 'SiteGridA',
        Columns  : [
            {
                Names    : [ { Name: 'left-bleed-start' } ],
                Type     : 'normal',
                CalcType : 'divide',
                CSize : { Size: 4 }
            },
            ...
        ],
        Rows     : [
            {
                Names    : [ { Name: 'top-bleed-start' } ],
                Type     : 'normal',
                CalcType : 'divide',
                CSize : { Size: 4 }
            },
            ...
        ]
    },
    {
        GridName : 'LinkContainerA',
        Columns  : [
            {
                Names    : [ { Name: 'main-link-col-start' } ],
                Type     : 'normal',
                CalcType : 'none',
                CSize : { Size: 0 }
            },
            ...
        ],
        Rows     : [
            {
                Names    : [ { Name: 'content-row-sart' } ],
                Type     : 'normal',
                CalcType : 'none',
                CSize : { Size: 0 }
            },
            ...
        ]
    }
]

The property GridName is what needs to match the GridStyle parameter in the Settings variable within the line of code causing the error.

Various attempts have been made like adding OnInit to ensure that the grid Data is loaded before the constructor is triggered. Tried different combinations of = signs, also experimented with swapping const for let. Despite these efforts, the issue still persists. The .find() method has been used in similar scenarios without issues. Any suggestions on how to resolve this?

Answer №1

Upon completing all the necessary parts, I encountered a different error compared to the one mentioned in your query:

const t: GridType   = gridData.find( a => { a.GridName == core.GridStyle; return a; } );

Argument of type '(this: void, a: GridType) => GridType' 
   is not assignable to parameter of type 
     '(value: GridType, index: number, obj: GridType[]) => boolean'.
          Type 'GridType' is not assignable to type 'boolean'.

The current error message is

Type 'GridType' is not assignable to type 'boolean'
. This issue arises because the function you are passing to find

{ a.GridName == core.GridStyle; return a; }

returns a, which is of type GridType, while find requires a function that returns a boolean.

The previous error from your question stated

Type 'void' is not assignable to type 'boolean'.

This could be because originally the code was

const t: GridType   = gridData.find( a => { a.GridName == core.GridStyle; } );

and when there is code within curly braces without a return statement, it defaults to return void.

To find a grid with a specific style, you should return the result of the comparison a.GridName == code.GridStyle. This can be achieved with a simple expression without braces after =>, such as:

const t: GridType   = gridData.find( a => a.GridName == core.GridStyle );

or with curly braces and a return statement that correctly returns the value of the correct type, like this:

const t: GridType   = gridData.find( a => { return a.GridName == core.GridStyle; } );

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

The entity is not validated by class-validator

Is it possible to utilize class-validator for validating columns in an Entity? The validation does not seem to work for columns: import { IsEmail } from 'class-validator'; @Entity() export class Admin extends BaseEntity { @Column({ unique: t ...

Struggling to locate all elements within a MongoDB TypeORM database collection

When utilizing the find and findOne methods to retrieve data from a collection, not all items are being returned. Collection.ts @Entity() export class Collection { @ObjectIdColumn() id !: string ; @Column() symbol !: string @Column ...

Is there a way to store session variables in Angular without the need to make an API call?

I am currently working with a backend in PHP Laravel 5.4, and I am looking for a way to access my session variables in my Angular/Ionic project similar to how I do it in my Blade files using $_SESSION['variable_name']. So far, I have not discove ...

What is the process for unsubscribing through an HTTP post request within an Angular application

There is a POST API set up through API Gateway on AWS, but it seems to be returning an infinite loop of arrays, as shown in the image below. How can I make it return just one array? Multiple requests Below is the code snippet: Angular import { Componen ...

Ways to properly link a webpage using the anchor tag's href attribute

I am currently working with reactjs and bootstrap, and I am trying to display and collapse an unordered list within a list item (nested unordered list). Bootstrap provides a useful collapse feature for this purpose. By using an anchor tag with a data-toggl ...

Submitting a form using an anchor tag in Angular 8: A step-by-step guide

I have a question about how to submit form data using hidden input fields when a user clicks on an <a> tag. <form action="/submit/form/link"> <input type="hidden" [attr.value]="orderNumber.id" /> <input type="hidden" [attr.value]= ...

Issue: The Karma plugin in Angular CLI >6.0 is now exported from "@angular-devkit/build-angular" instead

Issue: I'm encountering an error in Angular CLI version 6.0 or higher where the Karma plugin is now exported by "@angular-devkit/build-angular". // Below is the Karma configuration file, you can find more information at // module.exports = function ...

Issue: "contains method is not supported" in Ionic 2

I'm currently working on a code to validate the contents of my input field, but I've encountered an issue with using the contains function. Here's the TypeScript function I have written: checkFnameFunction(name){ if(name.contains("[a-z ...

How can a dialogue initiate itself automatically?

I am working with an Angular Material Design dialog component placed on a specific route, and I would like the dialog to automatically open when navigating to that route. Is there a method for the mdDialog to trigger its own opening without explicitly ref ...

Using custom properties from the Material-UI theme object with custom props in Emotion styled components: a step-by-step guide

I have implemented a custom object called fTokens into the MUI theme using Module augmentation in TypeScript This is how my theme.d.ts file is structured declare module "@mui/material/styles" { interface FPalette { ... } interface FTokens ...

Should every component be imported in app.module.ts?

Let's say I have three components: componentA, componentB, componentC. Is it necessary to import all three components? Why or why not? For example, in the following section of code in app.module.ts: app.module.ts @NgModule({ declarations: [** ...

distinguish different designs for individual components in Angular 5

My project is divided into two main parts: one for public web pages and the other for an admin control panel. Each part has its own set of CSS and javascript files for custom templates. If I include all CSS and js files in index.html, they all load at the ...

Encountering Typescript issues while trying to access @angular/core packages

Recently, I made an update to my Ionic app from Angular 7 to Angular 8, and an odd error popped up: https://i.sstatic.net/icZOb.png The issue lies in the fact that I am unable to access any of the standard classes stored in the @angular/core module. This ...

The issue arises when attempting to use the search feature in Ionic because friend.toLowerCase is not a valid function

I keep encountering an error message that says "friend.toLowerCase" is not a function when I use Ionic's search function. The unique aspect of my program is that instead of just a list of JSON items, I have a list with 5 properties per item, such as f ...

The Vuex MutationAction decorator cannot be assigned to a TypedPropertyDescriptor

I am a beginner in Typescript and apologize in advance if this is a rookie mistake. I am struggling to resolve this TS error: @Module({ namespaced: true, name: "Admin" }) class Admin extends VuexModule { public adminUserList: UserList = []; ...

The requested path /releases/add cannot be located

In my CRUD application, I have a feature that allows users to create releases by adding a version and description. This is achieved through a button and input fields for the details. <button (click)="addRelease(version.value, description.value)" [d ...

The error message "tsc not found after docker image build" appeared on the

My goal is to deploy this program on local host. When I manually run "npm run build-tsc," it works successfully. However, I would like Docker to automatically run this command when building the image. Unfortunately, I receive an error saying that tsc is no ...

What strategies prove most successful in resetting a reactive angular form effectively?

I'm currently working with Reactive Forms using Angular Material inputs (mdInput) that are initialized with FormBuilder in the following way: contactForm: FormGroup; this.contactForm = this.fb.group({ name: ['', [Validators.required, Val ...

Customized IntelliSense naming for overloaded parameters with conditional tuple types

In TypeScript 3.1, I have a generic function with arguments of either (TInput, string) or (string), depending on whether the generic's type parameter TInput extends undefined. To achieve this, I'm utilizing the new generic rest parameters feature ...

How can you ensure an interface in typescript 3.0 "implements" all keys of an enum?

Imagine I have an enum called E { A = "a", B = "b"}. I want to enforce that certain interfaces or types (for clarity, let's focus on interfaces) include all the keys of E. However, I also need to specify a separate type for each field. Therefore, usi ...