skip to Main Content

I am getting below error
"Argument of type ‘string | undefined’ is not assignable to parameter of type"
I don’t know how to solve that error.. here is my code

  export interface IDropDown {
  label: string;
  value: string;
}
export enum BOOKING_TYPE {
  FC = 'FC',
  LC = 'LC',
  TUFFING = 'TUFFING',
}
  export interface IImportBookingData {
      booking_type: IDropDown | null;
  }
  let bookingData: IImportBookingData;
  const iscl = [BOOKING_TYPE.LC, BOOKING_TYPE.TUFFING].indexOf(bookingData?.booking_type?.value) !== -1;

  console.log(iscl)

code Link

https://www.typescriptlang.org/play?ssl=17&ssc=1&pln=1&pc=1#code/ATCmA8AcHsCcBdgEsB29SwGYEMDGpgBJAEVmkmOgHcVgBvAWACgQAbbAI1FYC5gBneLFQBzANzMQAN2ysArqD6DhKccwC+zCDARgUcgLbAAQgHlTAaUIA5AOIB9ACoBNAAoBRepOAAxAMLAALzAAOT+IQA03gAyAcEhsZHejgCqPj42tkGhqemZSUyaLGBQcIio6Fh4BISEBjrwxtDQANaixNjw2F7FICAczW2q9vAAnpCKRKTklDTAAD7A+qysEsVFbKCIA63tndh8tfVlTbuqHV1rILjQKILI-Lis2QDaZpaZTm7uAHSxESZzFY7F8PD9chk7ABdH6oAAmEFMmAAFDshiILtgAPw-NGiEbjUA4mTyUAASmAAEJAsEALQARjW3hud2grFAP1Y0BEyKQj1YZOYQA

any idea ?

enter image description here

2

Answers


  1. The type booking_type is defined as IDropDown. But, later your are accessing the value prop of the booking_type inside the indexOf.

    Here, the value is string and it will show the type error. To overcome that, you need to type cast the value.

    const iscl = [BOOKING_TYPE.LC, BOOKING_TYPE.TUFFING]
       .indexOf((bookingData?.booking_type?.value) as BOOKING_TYPE) !== -1;
    

    Here the as BOOKING_TYPE will take care the type casting errors.

    Login or Signup to reply.
  2. export enum BOOKING_TYPE {
      FC = 'enumFC',
      LC = 'LC',
      TUFFING = 'TUFFING',
    }
    export interface IDropDown {
      label: string;
      value: BOOKING_TYPE; // value should be of type BOOKING_TYPE
    }
    export interface IImportBookingData {
        booking_type: IDropDown;
    }
    // assing value first
    let bookingData: IImportBookingData = {
      booking_type: 
      {label:"hello", value: 'LC'}
    };
    
    const iscl = [BOOKING_TYPE.LC, BOOKING_TYPE.TUFFING]
                         .indexOf(bookingData.booking_type.value) !== -1;
    
    console.log(iscl)
    

    Also check this solution

    // export enum BOOKING_TYPE {
    //   FC = 'enumFC',
    //   LC = 'LC',
    //   TUFFING = 'TUFFING',
    // }
    
    export type BOOKING_TYPE = "enumFC" | "LC" | "TUFFING";
    
    export interface IDropDown {
      label: string;
      value: BOOKING_TYPE;
    }
    export interface IImportBookingData {
        booking_type: IDropDown;
    }
    // assing value first
    let bookingData: IImportBookingData = {
      booking_type: 
      {label:"hello", value: 'LC'}
    };
    
    // const iscl = [BOOKING_TYPE.LC, BOOKING_TYPE.TUFFING]
    //                     .indexOf(bookingData.booking_type.value) !== -1;
    const iscl = ["LC", "TUFFING"].indexOf(bookingData.booking_type.value) !== -1;
    
    console.log(iscl)
    
    

    Hope this solves your problem.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search