Repository: TypeScript
Issue: Enable Generics to be inferred on assignment
Labels: Suggestion,Awaiting More Feedback

# Suggestion

This suggestion is to enable Generics to be inferred and defined in a single step.  This could look something like this:
```typescript
const x: InferredGeneric<*> = [....] as const 
// returning const x = [...] as const; x: InferredGeneric<typeof x>
```
The `<*>` indicates that the default type of the `[...] as const` should be passed as the first argument to `InferredGeneric` and `InferredGeneric`'s return type used as the final type for `x`.

Even better would be to combine this with [issue: #30680](https://github.com/microsoft/TypeScript/issues/30680):
```typescript
// probably an even better solution would be to enable it to be used with `as const`:
const x: InferredGeneric<* as const> = [...]
```

## 🔍 Search Terms

"infer generic type" 

## ✅ Viability Checklist

<!--
   Suggestions that don't meet all these criteria are very, very unlikely to be accepted.
   We always recommend reviewing the TypeScript design goals before investing time writing
   a proposal for ideas outside the scope of the project.
-->
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, new syntax sugar for JS, etc.)
* [✅ ] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).


## ⭐ Suggestion

Enhance generics to accept `<*>` or `<* as const>` when they are used and this would mean the default type of the variable being defined is passed to the generic and whatever it returns would be the final variable type.

## 📃 Motivating Example

If generics cannot be defined and inferred in a single step, then it breaks the DRY principle as one would have to do the following:
```typescript
type GenericFn<T extends unknown[]> = (...args: T) => number | string
type InferGenericFn<T extends ((...args: any) => number | string)> = T extends (...args: infer A) => number | string ? GenericFn<A> : never
type InferGenericFnArray<T extends readonly unknown[]> = T extends readonly [...genericFns: infer P] ? { [I in keyof P]: P[I] extends  ((...args: any) => number | string) ?  InferGenericFn<P[I]> : never} : never

const fnArray = [
  (value: number) => value,
  (text: string) => parseInt(text, 10),
] as const

// STEP 1: infer generic
type InferredGenericFnArray = InferGenericFnArray<typeof fnArray>

// STEP 2: apply inferred generic to array for type checking
const fnArrayNotDRY: InferredGenericFnArray  = [
  (value: number) => `${value}`,
  (text: string) => true,
]
```

This solution would combine STEP 1 and STEP 2 into a single easy to read and understand step, and so would become:

```typescript
type GenericFn<T extends unknown[]> = (...args: T) => number | string
type InferGenericFn<T extends ((...args: any) => number | string)> = T extends (...args: infer A) => number | string ? GenericFn<A> : never
type InferGenericFnArray<T extends readonly unknown[]> = T extends readonly [...genericFns: infer P] ? { [I in keyof P]: P[I] extends  ((...args: any) => number | string) ?  InferGenericFn<P[I]> : never} : never

// STEP 1&2: combined in a single step
const fnArray: InferredGenericFnArray<* as const> = [
  (value: number) => value,
  (text: string) => parseInt(text, 10),
]
```



[code](https://www.typescriptlang.org/play?#code/C4TwDgpgBA4hB2EBOBLAxgMXgHgCpQgA9gEATAZygFd4BreAewHd4BtAXQD4oBeKACgB0wgIZIA5uQBcUXAEpe3eFQC2AI2RQAPlHLBU8cQChQkKAEl4AM2RxEqTDnxES8CgKGiJ0qCPggFHiVVDSRtXX0UQzluPmdiMkpPQTFJGSibMIBBQOD1TR09A3EoAH5YBGR0LGws7hlEADdkE3BoS0y7KscspCQREDwCBLdKJAgRUgZ4ABsQajpGFg5Y2WHXd3HJ6bmoVmFBcUqHLB8MzQAFdjKoAG898ygoqFoIEAYrKCuZC9Zza5ciSgHgOqR8fgCiigynyYUKkWiNws1lsx2qOF+-3q0IgzSQAF8oA1cS0jAB6MlQNDTPRPFFILoOXr9eZ8KBDQGjKBbKazeb8ZJgmQQ3LQkIFCLFOQrfhWeA+fCiuWUESUGj0ZjwXyUDqo+zo5kDPCcADcRiM1PlwCgcsNrL2RmB-EaIhmVAgDXFSFFLrdEAANI6BCRiDIilFxKKwGJyBBLMB+CHgP6oABGAAMckD11VVJpwHNpna9PGpEZBr6A14yM6aJ6lcGRY+Nvgds45sttNtDYAcgxgAARABKAE0ZLq+hAy3WsHbgXxWEHna73Z7YaKAAYAEluvvd+I3gadSbDCMjUP07uz5vJlKyugYbuAKGmUBUVY0UGADAI8BEahmaAjn1NBKG-Xw0DQCAwGtYAAAtoFICArBEKgZlgtooGbeDoBdVB-0AqANAjbVyBQcREFIQRgQAUUOajnhwqAwFjKgpjzJCiIgGZmFvWQEKgJCULQjCzGbbsWSeSho3IWNSG1L8BKsFAkFpaN+hUCASDCcCNwnUty3rFkN0U6AN2wAAqTgTKiUh0BEZ9DEUlBKHIOCGDQ+TP15CBBAtfMWztccSynQzZwbSzVkXJ09w9MV1yhWKj2DFxTylKF1NjeNExcFMMyzIwc0oTsCz4sAkAYNQCPmPwCGaLUNGAbSHyfF8tSYDyZi86BwIQAjoBQWCf0-Kg5KgJhBrgqAN1zEqNykfyrUChtgsyAyZ1bCKLIUkqoqXWK11CH0VwDJcT0lCMoxjON4ATJM8szbMgA)

## 💻 Use Cases

Makes generics more powerful by inferred them and applied the resultant definition in a single step.