Repository: TypeScript
Issue: Keyword to permit inferring a union for a type parameter
Labels: Suggestion,In Discussion

# Suggestion

## 🔍 Search Terms

- Widen
- Generics
- Open

## ✅ Viability Checklist

My suggestion meets these guidelines:

* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).

## ⭐ Suggestion

Right now TypeScript has some rules about when it decides to widen a generic type or not, which can produce confusing results for relatively similar function signatures:

```ts
declare function fn1<T>(items: Array<T>): T
declare function fn2<T>(items: Array<() => T>): T
declare function fn3<T>(items: Array<{ prop: T }>): T

let res1: number | string = fn1([42, "hi"])
let res2: number | string = fn2([() => 42, () => "hi"]) // string ("hi") is not a number
let res3: number | string = fn3([{ prop: 42 }, { prop: "hi" }]) // string ("hi") is not a number
```

Right now its entirely outside of your control what TypeScript will decide to do when a type parameter is not provided and what TypeScript does has changed a few times over the years.

It would help a lot if TypeScript gave you some more control over this behavior, to specify if you want your generics to widen or not.

> Please ignore the syntax, just demonstrating where I'd expect it to go

```ts
// widen
declare function fn<widen T>(items: Array<() => T>): T;
fn([() => 1, () => 2, () => "three"]) // => number | string

// do not widen
declare function fn<donotwiden T>(items: Array<T>): T;
fn([1, 2, "three"]) // ERR
```

## 📃 Motivating Example

An increasingly popular way to use TypeScript is to produce types from runtime values, there are already lots of TypeScript features dedicated to making it easier to infer precise types from values, and there are even libraries like [zod](https://github.com/colinhacks/zod) and io-ts to do things like:

```js
let type = union(string(), number())

let value: unknown = ...
assert(value, type)
value // >> string | number
```

Libraries written in TypeScript are increasingly relying on TypeScript's value-to-type inference as part of their public API. So when TypeScript changes its rules about things like when to widen vs not widen, it can be much more dramatic of a breaking change that sometimes requires redesigning these libraries.

```ts
type Check<T> = (value: unknown) => value is T

function string(): Check<string> {...}
function number(): Check<number> {...}
function union<T>(...members: Assertion<T>[]): Check<T> {...}

let assert = union(string(), number()) // ERR: number is not a string
let assert = union<string | number>(string(), number())
```

Right now there are a couple hacks you can do to trick TypeScript into having the desired widening behavior (by lying about the actual types). But there is no guarantee that this behavior will stick around between TypeScript versions.

## 💻 Use Cases

```ts
type Check<T> = (value: unknown) => value is T

function string(): Check<string> {...}
function number(): Check<number> {...}
function union<widen T>(...members: Check<T>[]): Check<T> {...}

let assert1 = union(string(), number())
let assert2 = union<string | number>(string(), number())
type SomeUnionToStayInSyncWith = string | number
let assert3 = union<SomeUnionToStayInSyncWith>(string(), number())
```
[[Playground](https://www.typescriptlang.org/play?#code/C4TwDgpgBAwgFhAxgawDwBUB8UC8UAUAbgIYA2ArhAFxTkB2ydA9gO50CUu2JF0AlgGco6ALAAocQDN6iYHyZ0oA4ACc+dAOb52NeEjTK1m7AG9xUKCojByKxUTKUdUHpSiClq9Rq5RQkJkkXR2gcMKgAIkNvCPEAX3EpGTkFKDpyAFsAIwgVbV0EFFR07NzTc0trW3tXCGda9yESnJVffwhA4N5ccIjm3NixBIkxaTpZeUV6SYxMfAA6RYyIUpUBAv1ZgG0AXWc9IqwoMzELKxs7Alr6kMbhXxOLC0kmVvxSayhl1ahO75aBJxHk8LHwgvh-rkHLx2ECKiCzlVLqpKPCnsMQRinudqlBJGQBBAKsNhuIPsAoMQBISVMAAIy4Wh0Sb4aKabQAGjSmRa2k4AHp+VAAKIAJVFNH6rQ8zApxE8Rg0ZM+VJpwAATIzpgpUGyfAAfbmrOZ6zlG3mwxJidpQADKTGWAFVmQp0ExbcBiCAAJJ0W0gcYAdT4wDgjL1UENUuVcupuWAAGYtS66Kh7U6U26PV7ff6gyG4CavOz2FypXzxEA)]