How do I type a function's arguments but leave the return type "dynamic"?

Viewed 17

Below I have two functions originalhelloWorld which is untyped and helloWorld which has a type. You can see that the return of type o returns the "dynamic" return type (what is the name for this), and type x returns "any".

How can I have the ExampleFunction type the functions arguments but leave the return type dynamic? I've tried several combinations of generics, and nothing seems to work.

Typescript Playground

const originalhelloWorld = (greeting: string | boolean) => {
   if (typeof greeting === 'boolean') return greeting
   return `hello ${greeting}`
}

type o = ReturnType<typeof originalhelloWorld>
//  ^? type o = string | boolean

/* ------------------------------------ */

type ExampleFunction = (greeting: string | boolean) => any

const helloWorld: ExampleFunction = (greeting) => {
   if (typeof greeting === 'boolean') return greeting
   return `hello ${greeting}`
}

type x = ReturnType<typeof helloWorld>
//  ^? type x = any
0 Answers
Related