Skip to content

Commit b93c25b

Browse files
committed
filter using getiterationtypeofgeneratorfunctionreturntype
1 parent 236e10e commit b93c25b

File tree

3 files changed

+5
-12
lines changed

3 files changed

+5
-12
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26777,11 +26777,9 @@ namespace ts {
2677726777
if (contextualReturnType) {
2677826778
const functionFlags = getFunctionFlags(func);
2677926779
if (functionFlags & FunctionFlags.Generator) { // Generator or AsyncGenerator function
26780+
const isAsyncGenerator = (functionFlags & FunctionFlags.Async) !== 0;
2678026781
if (contextualReturnType.flags & TypeFlags.Union) {
26781-
contextualReturnType = filterType(contextualReturnType, type => {
26782-
const generator = createGeneratorReturnType(anyType, anyType, anyType, (functionFlags & FunctionFlags.Async) !== 0);
26783-
return checkTypeAssignableTo(generator, type, /*errorNode*/ undefined);
26784-
});
26782+
contextualReturnType = filterType(contextualReturnType, type => !!getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Return, type, isAsyncGenerator));
2678526783
}
2678626784
const iterationReturnType = getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Return, contextualReturnType, (functionFlags & FunctionFlags.Async) !== 0);
2678726785
if (!iterationReturnType) {
@@ -26816,16 +26814,11 @@ namespace ts {
2681626814
const func = getContainingFunction(node);
2681726815
if (func) {
2681826816
const functionFlags = getFunctionFlags(func);
26819-
// let contextualReturnType = getContextualReturnType(func);
26820-
// const contextualReturnType = getContextualReturnType(func, contextFlags);
2682126817
let contextualReturnType = getContextualReturnType(func, contextFlags);
2682226818
if (contextualReturnType) {
2682326819
const isAsyncGenerator = (functionFlags & FunctionFlags.Async) !== 0;
2682426820
if (!node.asteriskToken && contextualReturnType.flags & TypeFlags.Union) {
26825-
contextualReturnType = filterType(contextualReturnType, type => {
26826-
const generator = createGeneratorReturnType(anyType, anyType, anyType, isAsyncGenerator);
26827-
return checkTypeAssignableTo(generator, type, /*errorNode*/ undefined);
26828-
});
26821+
contextualReturnType = filterType(contextualReturnType, type => !!getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Return, type, isAsyncGenerator));
2682926822
}
2683026823
return node.asteriskToken
2683126824
? contextualReturnType

tests/baselines/reference/contextualTypeOnYield1.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type FuncOrGeneratorFunc = () => (number | Generator<(arg: number) => void, any,
55

66
const f: FuncOrGeneratorFunc = function*() {
77
>f : FuncOrGeneratorFunc
8-
>function*() { yield (num) => console.log(num); // `num` should be inferred to have type `number`.} : () => Generator<(num: number) => void, void, any>
8+
>function*() { yield (num) => console.log(num); // `num` should be inferred to have type `number`.} : () => Generator<(num: number) => void, void, unknown>
99

1010
yield (num) => console.log(num); // `num` should be inferred to have type `number`.
1111
>yield (num) => console.log(num) : any

tests/baselines/reference/contextualTypeOnYield2.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type OrGen = () => (number | Generator<string, (arg: number) => void, undefined>
55

66
const g: OrGen = function* () {
77
>g : OrGen
8-
>function* () { return (num) => console.log(num);} : () => Generator<never, (num: number) => void, any>
8+
>function* () { return (num) => console.log(num);} : () => Generator<never, (num: number) => void, unknown>
99

1010
return (num) => console.log(num);
1111
>(num) => console.log(num) : (num: number) => void

0 commit comments

Comments
 (0)