Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Typescript function overload implicitly has any type

type UpdateConsumerT = {
  obj: {
    min_quantity: number
    fixed_price: number
    date_start: string | boolean
    date_end: string | boolean
  }
}

type UpdateProducerT = {
  obj: {
    min_qty: number
    price: number
    date_start: string | boolean
    date_end: string | boolean
  }
}

export async function updatePricelistItem(
  pricelist_item_id: number,
  obj: UpdateConsumerT,
  isProduction: false
): Promise<number>
export async function updatePricelistItem(
  pricelist_item_id: number,
  obj: UpdateProducerT,
  isProduction: true
): Promise<number>
export async function updatePricelistItem(
  pricelist_item_id,
  obj,
  isProduction = false
) { ... }

I have this function, and i try to use overloads

But for some reason it says

Parameter ‘pricelist_item_id’ implicitly has an ‘any’ type.ts(7006)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

My question is, what am i doing wrong here?

>Solution :

You will need to explicitly specify the parameter types and return type on the implementation:

type UpdateConsumerT = {
  obj: {
    min_quantity: number
    fixed_price: number
    date_start: string | boolean
    date_end: string | boolean
  }
}

type UpdateProducerT = {
  obj: {
    min_qty: number
    price: number
    date_start: string | boolean
    date_end: string | boolean
  }
}

async function updatePricelistItem(
  pricelist_item_id: number,
  obj: UpdateConsumerT,
  isProduction: false
): Promise<number>
async function updatePricelistItem(
  pricelist_item_id: number,
  obj: UpdateProducerT,
  isProduction: true
): Promise<number>
async function updatePricelistItem(
  pricelist_item_id: number,
  obj: UpdateProducerT | UpdateConsumerT,
  isProduction = false
): Promise<number> { return new Promise(() => {}) }

Playground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading