tc39 / proposal-flatMap

proposal for flatten and flatMap on arrays
https://tc39.github.io/proposal-flatMap
214 stars 19 forks source link

.d.ts proposal inaccurate #76

Closed shicks closed 5 years ago

shicks commented 5 years ago

The proposed .d.ts for Array.prototype.flat is much less accurate than it could be.

The following is more in line with the implementation:

type Unwrap<X> = X extends (infer Y)[] ? Y : X;

interface Array<T> {
    // ...
    flat(depth: 2): Unwrap<Unwrap<Unwrap<this>>>[];
    flat(depth?: 1): Unwrap<Unwrap<this>>[];
    flat(depth: 0): Unwrap<this>[];
    flat(depth?: number): any[];
}

This allows correctly inferring that calling flat(1) on, e.g., (number | number[][])[] returns (number | number[])[].

bakkot commented 5 years ago

That file is no longer particularly relevant. (Nor is this repository, for that matter.) TypeScript has its own definitions, and any issues with them should be raised with the TypeScript team, not here.