From 24029cc4f3f9a5954d39df9eeb5d1de3066bcfcf Mon Sep 17 00:00:00 2001 From: Alexey Gerasimov Date: Mon, 23 Dec 2019 22:50:18 +0500 Subject: [PATCH] Fix flatten() return type to properly handle shallow parameter on type level (#41099) --- types/lazy.js/index.d.ts | 105 ++++++++++++++++++--------------- types/lazy.js/lazy.js-tests.ts | 5 ++ 2 files changed, 64 insertions(+), 46 deletions(-) diff --git a/types/lazy.js/index.d.ts b/types/lazy.js/index.d.ts index 1e849d1c6f..e0ad020409 100644 --- a/types/lazy.js/index.d.ts +++ b/types/lazy.js/index.d.ts @@ -3,8 +3,9 @@ // Definitions by: Bart van der Schoor // Mike Doughty // Gabriel Lorquet +// Alexey Gerasimov // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.1 +// TypeScript Version: 2.9 declare namespace LazyJS { interface LazyStatic { @@ -13,121 +14,132 @@ declare namespace LazyJS { (value: any[]): ArrayLikeSequence; (value: any): ObjectLikeSequence; (value: any): ObjectLikeSequence; - + strict(): LazyStatic; - + generate( generatorFn: GeneratorCallback, length?: number ): GeneratedSequence; - + range(to: number): GeneratedSequence; range(from: number, to: number, step?: number): GeneratedSequence; - + repeat(value: T, count?: number): GeneratedSequence; - + on(eventType: string): Sequence; - + readFile(path: string): StringLikeSequence; makeHttpRequest(path: string): StringLikeSequence; } - + interface ArrayLike { length: number; [index: number]: T; } - + interface Callback { (): void; } - + interface ErrorCallback { (error: any): void; } - + interface ValueCallback { (value: T): void; } - + interface GetKeyCallback { (value: T): string; } - + interface TestCallback { (value: T, index: U): boolean; } - + interface MapCallback { (value: T): U; } - + interface MapStringCallback { (value: string): string; } - + interface NumberCallback { (value: T): number; } - + interface MemoCallback { (memo: U, value: T): U; } - + interface GeneratorCallback { (index: number): T; } - + interface CompareCallback { (x: any, y: any): number; } - + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + interface Iterator { new (sequence: Sequence): Iterator; current(): T; moveNext(): boolean; } - + interface GeneratedSequence extends Sequence { new (generatorFn: GeneratorCallback, length: number): GeneratedSequence< T >; length(): number; } - + interface AsyncSequence extends SequenceBase { each(callback: ValueCallback): AsyncHandle; } - + interface AsyncHandle { cancel(): void; onComplete(callback: Callback): void; onError(callback: ErrorCallback): void; } - + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + namespace Sequence { function define(methodName: string[], overrides: any): Function; } - + interface Sequence extends SequenceBase { each(eachFn: ValueCallback): Sequence; } - + interface SequenceBase extends SequenceBaser { first(): any; first(count: number): Sequence; indexOf(value: any, startIndex?: number): number; - + last(): any; last(count: number): Sequence; lastIndexOf(value: any): number; - + reverse(): Sequence; } - + + type Flatten = + Shallow extends true + ? T extends Sequence + ? U + : T + // workaround for https://github.com/microsoft/TypeScript/issues/26980 + : { + 0: T extends Sequence ? Flatten : never; + 1: T; + } [T extends Sequence ? 0 : 1]; + interface SequenceBaser { // TODO improve define() (needs ugly overload) async(interval: number): AsyncSequence; @@ -143,7 +155,8 @@ declare namespace LazyJS { filter(predicateFn: TestCallback): Sequence; find(predicateFn: TestCallback): T; findWhere(properties: any): T; - flatten(): Sequence; + flatten(shallow: true): Sequence>; + flatten(shallow?: false): Sequence>; groupBy(keyFn: GetKeyCallback): ObjectLikeSequence; initial(count?: number): Sequence; intersection(var_args: T[]): Sequence; @@ -177,13 +190,13 @@ declare namespace LazyJS { without(var_args: T[]): Sequence; zip(var_args: T[]): Sequence; } - + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + namespace ArrayLikeSequence { function define(methodName: string[], overrides: any): Function; } - + interface ArrayLikeSequence extends Sequence { // define()X; concat(var_args: T[]): ArrayLikeSequence; @@ -199,7 +212,7 @@ declare namespace LazyJS { shift(): ArrayLikeSequence; slice(begin: number, end?: number): ArrayLikeSequence; unshift(value: T): ArrayLikeSequence; - + dropWhile(predicateFn: TestCallback): Sequence; every(predicateFn: TestCallback): boolean; filter(predicateFn: TestCallback): Sequence; @@ -209,13 +222,13 @@ declare namespace LazyJS { some(predicateFn?: TestCallback): boolean; takeWhile(predicateFn: TestCallback): Sequence; } - + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + namespace ObjectLikeSequence { function define(methodName: string[], overrides: any): Function; } - + interface ObjectLikeSequence extends Sequence { assign(other: any): ObjectLikeSequence; // throws error @@ -233,7 +246,7 @@ declare namespace LazyJS { toObject(): any; values(): Sequence; watch(propertyNames: string | string[]): Sequence<{property: string; value: any;}>; - + dropWhile(predicateFn: TestCallback): Sequence; every(predicateFn: TestCallback): boolean; filter(predicateFn: TestCallback): Sequence; @@ -243,13 +256,13 @@ declare namespace LazyJS { some(predicateFn?: TestCallback): boolean; takeWhile(predicateFn: TestCallback): Sequence; } - + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + namespace StringLikeSequence { function define(methodName: string[], overrides: any): Function; } - + interface StringLikeSequence extends SequenceBaser { charAt(index: number): string; charCodeAt(index: number): number; @@ -281,10 +294,10 @@ declare namespace LazyJS { takeWhile(predicateFn: TestCallback): Sequence; } } - + declare var Lazy: LazyJS.LazyStatic; - + declare module 'lazy.js' { export = Lazy; } - + diff --git a/types/lazy.js/lazy.js-tests.ts b/types/lazy.js/lazy.js-tests.ts index 5df3aabf1f..3f7d5476d6 100644 --- a/types/lazy.js/lazy.js-tests.ts +++ b/types/lazy.js/lazy.js-tests.ts @@ -225,3 +225,8 @@ stringSeq = stringSeq.substring(num); stringSeq = stringSeq.substring(num, num); stringSeq = stringSeq.toLowerCase(); stringSeq = stringSeq.toUpperCase(); + +// flatten +var fooSeqSeqSequence: LazyJS.Sequence>>; +fooSequence = fooSeqSeqSequence.flatten(); +fooSequence = fooSeqSeqSequence.flatten(true).flatten(true);