#`typeof` declines anonymous function
32 messages ยท Page 1 of 1 (latest)
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
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']>.
or just
const someFn = () => db.fn()
type FnReturnType = ReturnType<typeof someFn>
```since the function will not be called?
yeah.. buuut it will hang in the compiled js, right? ๐๐
yeah ๐
Maybe you can provide more context about what you're actually trying to do?
I simply wanted to get the awaited type of a prisma query with complex nested selectors, though I was not going to use this function. Sounds weird but that's what it is
@wind rivet Well, if you don't mind a dirty hack:
const hackery = (false as true) && db.fn(/*args*/);
type YourType = typeof hackery;
!resolved
@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
oh, was instantiation expression available on typeof?
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.
Preview:ts declare const f: <T>() => T type T = typeof f<0>
Preview:ts declare const f: <T>() => T type T = typeof f<0>
it seems like it is an instantiation expression since instantiation expressions are released on TS 4.7 ๐ค
on TS 4.6, it is a syntax error
Preview:ts declare const f: <T>() => T type F = typeof f type T = F<0>
// 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)
type F<T> = <U>() => U & T
// ^? - type F<T> = <U>() => U & T
type F1 = F<0>
// ^? - type F1 = <U>() => U & 0