#`typeof` declines anonymous function

32 messages ยท Page 1 of 1 (latest)

wind rivet
#

I thought that would be possible to give typeof an anonymous function

#

it seems, typescript does not work the way I wanted

#

I'd like to get an explanation why it's not possible. I would not like to create an individual function just to feed it to a typeof and never use

#

typeof declines anonymous function

proper chasm
#

typeof can only be used on an identifier, not an expression.

#

Instead of writing ReturnType<typeof () => db.fn()> you can do something like ReturnType<(typeof db)['fn']>.

quaint path
#

or just

const someFn = () => db.fn()
type FnReturnType = ReturnType<typeof someFn>
```since the function will not be called?
wind rivet
quaint path
#

yeah ๐Ÿ˜“

misty stump
#

Maybe you can provide more context about what you're actually trying to do?

wind rivet
misty stump
#

@wind rivet Well, if you don't mind a dirty hack:

const hackery = (false as true) && db.fn(/*args*/);
type YourType = typeof hackery;
wind rivet
#

!resolved

karmic umbra
#

@wind rivet If you write out the function and hover over it, you can see the generic type arguments that have been inferred

#

Then you can just write typeof function with that generic type argument

#

no hacks required

quaint path
#

oh, was instantiation expression available on typeof?

karmic umbra
#

It's not an instantiation expression. It's just a generic type.

#

typeof func is a generic type, and we can use Generic<T> with that generic type, ReturnType<GenericFunction<T>> in this case.

molten fogBOT
#
andjsrk#0

Preview:ts declare const f: <T>() => T type T = typeof f<0>

#
andjsrk#0

Preview:ts declare const f: <T>() => T type T = typeof f<0>

quaint path
#

it seems like it is an instantiation expression since instantiation expressions are released on TS 4.7 ๐Ÿค”

quaint path
karmic umbra
#

Really?

#

Perhaps you are right

molten fogBOT
#
sandiford#0

Preview:ts declare const f: <T>() => T type F = typeof f type T = F<0>

karmic umbra
#
// TS 4.6.4
declare const f: <T>() => T
type F = typeof f // <T>() => T
type T = F<0> // Type 'F' is not generic.(2315)
molten fogBOT
#
type F<T> = <U>() => U & T
//   ^? - type F<T> = <U>() => U & T
type F1 = F<0>
//   ^? - type F1 = <U>() => U & 0