Skip to content

Commit

Permalink
fix: Fix typing of conditional promises in pipeline
Browse files Browse the repository at this point in the history
Co-authored-by: Mikko Aspiala <[email protected]>
Signed-off-by: Janne Savolainen <[email protected]>
  • Loading branch information
jansav and Iku-turso committed Jan 23, 2024
1 parent f9ce968 commit ae89f00
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/fp/src/pipeline/pipelineFor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ type ContainsPromise<T extends [...any[]]> = T extends [
infer Head,
...infer Tail
]
? Head extends Promise<any>
? true
? Promise<any> extends Head
? {} extends Head
? false
: true
: ContainsPromise<Tail>
: false;

Expand Down
40 changes: 40 additions & 0 deletions packages/fp/src/pipeline/unsafePipeline/pipeline.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,43 @@ expectType<Promise<string>>(pipelineResultWithNoAsyncChanceOfBreak);
expectNotAssignable<Promise<typeof pipelineBreak>>(
pipelineResultWithNoAsyncChanceOfBreak,
);

const pipelineResultWithConditionalType = pipeline(
'some-string',

someParameter => {
expectType<string>(someParameter);

return 1 > 2
? Promise.resolve('some-resolved-string')
: 'some-other-string';
},

someParameter => {
expectType<string>(someParameter);

return someParameter;
},
);

expectType<Promise<string>>(pipelineResultWithConditionalType);

const pipelineResultWithConditionalTypeAndBreak = pipeline(
'some-string',

someParameter => {
expectType<string>(someParameter);

return 1 > 2
? Promise.resolve('some-resolved-string')
: 'some-other-string';
},

() => pipelineBreak,
);

expectType<Promise<symbol>>(pipelineResultWithConditionalTypeAndBreak);

const pipelineUsingEmptyObject = pipeline({}, x => x);

expectNotAssignable<{ then: any }>(pipelineUsingEmptyObject);

0 comments on commit ae89f00

Please sign in to comment.