Closed
Description
Search Terms
generic typeof functions
Suggestion
The problem is with the code as below:
const id = <T>(val: T) => val
type IdType = typeof id
// type IdType = <T>(val: T) => T
type Ret = ReturnType<typeof id>
// type Ret = unknown
It would be nice to be able to do something like so:
const id = <T>(val: T) => val
type IdType<T> = (typeof id)<T>
// type IdType<T> = (val: T) => T
// Or by extension:
type Ret<T> = ReturnType<(typeof id)<T>>
// type Ret<T> = T
Use Cases
This would be useful to use with the vriad/zod library, to implement different field validators depending on a parameter, like so:
// the dateSchema param could be a schema for an ISO 8601 string or a Date object, or even some custom date-like type
const fooSchema = <T>(dateSchema: z.ZodSchema<T>) => z.object({
name: z.string(),
date: dateSchema
})
type Foo<T> = z.infer<ReturnType<(typeof fooSchema)<T>>>
// expected result: type Foo<T> = { name: string, date: T }
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.