Here are two functions for you to explore: originalhelloWorld
, which is untyped, and helloWorld
, which has a specified type. Notice how the return type of the untyped function is inferred (what is this called?), while the typed function returns "any".
Is there a way to define the type of arguments for the ExampleFunction
but still leave the return type as inferred? I've experimented with generics in various combinations, but nothing seems to be effective.
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