简体   繁体   中英

Define the type of error in function argument

How do I define the type of error below?

async function falseThrow (promise: Promise<any>, error): any {
  let value: any = await promise
  if (!value) throw error
  return value
}

Exactly as you defined the type of promise being Promise<any> . You use : .

That depends on the type you want. If error is expected to be a string, then it would be:

async function falseThrow (promise: Promise<any>, error:string): any {
  let value: any = await promise
  if (!value) throw error
  return value
}

EDIT

Or you could use Error , if that's the case, as you stated on your own answer.

This works! Whoops, thought it did not.

async function falseThrow (promise: Promise<any>, error: Error): any {
  let value: any = await promise
  if (!value) throw error
  return value
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM