Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 28 additions & 23 deletions tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6497,18 +6497,37 @@ func (c *Checker) getIterationTypesOfIterable(t *Type, use IterationUse, errorNo
}
noCache = true
}
result := c.getIterationTypesOfIterableWorker(t, use, errorNode)
// Keep the original use site while probing union constituents without iterable diagnostics.
getAsyncFromSyncIterationTypes := func(iterationTypes IterationTypes) IterationTypes {
if iterationTypes == (IterationTypes{c.anyType, c.anyType, c.anyType}) {
return iterationTypes
}
if errorNode != nil {
c.getGlobalAwaitedSymbol()
}
diagnostic := core.IfElse(use&IterationUseYieldStarFlag != 0,
diagnostics.Type_of_iterated_elements_of_a_yield_Asterisk_operand_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member,
diagnostics.Type_of_await_operand_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member)
yieldType := c.getAwaitedTypeEx(iterationTypes.yieldType, errorNode, diagnostic)
returnType := c.getAwaitedTypeEx(iterationTypes.returnType, errorNode, diagnostic)
// A cached fallback to any would hide the error at subsequent uses of this iterable.
if yieldType == nil || returnType == nil {
noCache = true
}
return IterationTypes{core.OrElse(yieldType, c.anyType), core.OrElse(returnType, c.anyType), iterationTypes.nextType}
}
result := c.getIterationTypesOfIterableWorker(t, use, errorNode, getAsyncFromSyncIterationTypes)
if !noCache {
c.iterationTypesCache[key] = result
}
return result
}

func (c *Checker) getIterationTypesOfIterableWorker(t *Type, use IterationUse, errorNode *ast.Node) IterationTypes {
func (c *Checker) getIterationTypesOfIterableWorker(t *Type, use IterationUse, errorNode *ast.Node, getAsyncFromSyncIterationTypes func(IterationTypes) IterationTypes) IterationTypes {
if t.flags&TypeFlagsUnion != 0 {
allIterationTypes := make([]IterationTypes, 0, len(t.Types()))
for _, constituent := range t.Types() {
iterationTypes := c.getIterationTypesOfIterableWorker(constituent, use, nil)
iterationTypes := c.getIterationTypesOfIterableWorker(constituent, use, nil, getAsyncFromSyncIterationTypes)
if !iterationTypes.hasTypes() {
if errorNode != nil {
c.addDeferredDiagnostic(func() {
Expand All @@ -6526,7 +6545,7 @@ func (c *Checker) getIterationTypesOfIterableWorker(t *Type, use IterationUse, e
iterationTypes := c.getIterationTypesOfIterableFast(t, c.asyncIterationTypesResolver)
if iterationTypes.hasTypes() {
if use&IterationUseForOfFlag != 0 {
return c.getAsyncFromSyncIterationTypes(iterationTypes, errorNode)
return getAsyncFromSyncIterationTypes(iterationTypes)
}
return iterationTypes
}
Expand All @@ -6544,7 +6563,7 @@ func (c *Checker) getIterationTypesOfIterableWorker(t *Type, use IterationUse, e
iterationTypes := c.getIterationTypesOfIterableFast(t, c.syncIterationTypesResolver)
if iterationTypes.hasTypes() {
if use&IterationUseAllowsAsyncIterablesFlag != 0 {
return c.getAsyncFromSyncIterationTypes(iterationTypes, errorNode)
return getAsyncFromSyncIterationTypes(iterationTypes)
}
return iterationTypes
}
Expand All @@ -6556,7 +6575,7 @@ func (c *Checker) getIterationTypesOfIterableWorker(t *Type, use IterationUse, e
}
}
if use&IterationUseAllowsAsyncIterablesFlag != 0 {
return c.getAsyncFromSyncIterationTypes(iterationTypes, errorNode)
return getAsyncFromSyncIterationTypes(iterationTypes)
}
return iterationTypes
}
Expand Down Expand Up @@ -6653,22 +6672,6 @@ func (c *Checker) getIterationTypeUnion(iterationTypes []IterationTypes, f func(
return c.getUnionType(types)
}

func (c *Checker) getAsyncFromSyncIterationTypes(iterationTypes IterationTypes, errorNode *ast.Node) IterationTypes {
if !iterationTypes.hasTypes() ||
iterationTypes.yieldType == c.anyType && iterationTypes.returnType == c.anyType && iterationTypes.nextType == c.anyType {
return iterationTypes
}
// if we're requesting diagnostics, report errors for a missing `Awaited<T>`.
if errorNode != nil {
c.getGlobalAwaitedSymbol()
}
return IterationTypes{
yieldType: core.OrElse(c.getAwaitedTypeEx(iterationTypes.yieldType, errorNode, nil), c.anyType),
returnType: core.OrElse(c.getAwaitedTypeEx(iterationTypes.returnType, errorNode, nil), c.anyType),
nextType: iterationTypes.nextType,
}
}

// Gets the *yield*, *return*, and *next* types of an `Iterable`-like or `AsyncIterable`-like
// type from its members.
//
Expand Down Expand Up @@ -21018,7 +21021,9 @@ func (c *Checker) checkAndAggregateYieldOperandTypes(fn *ast.Node, checkMode Che
if yieldExpr.Expression() != nil && c.isConstContext(yieldExpr.Expression()) {
yieldExprType = c.getRegularTypeOfLiteralType(yieldExprType)
}
yieldTypes = core.AppendIfUnique(yieldTypes, c.getYieldedTypeOfYieldExpression(yieldExpr, yieldExprType, c.anyType, isAsync))
if yieldType := c.getYieldedTypeOfYieldExpression(yieldExpr, yieldExprType, c.anyType, isAsync); yieldType != nil {
yieldTypes = core.AppendIfUnique(yieldTypes, yieldType)
}
var nextType *Type
if yieldExpr.AsYieldExpression().AsteriskToken != nil {
iterationTypes := c.getIterationTypesOfIterable(yieldExprType, core.IfElse(isAsync, IterationUseAsyncYieldStar, IterationUseYieldStar), yieldExpr.Expression())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
yieldStarSyncIterableInvalidThenable.ts(7,12): error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member.
yieldStarSyncIterableInvalidThenable.ts(8,11): error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member.
yieldStarSyncIterableInvalidThenable.ts(9,31): error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member.
yieldStarSyncIterableInvalidThenable.ts(11,12): error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member.
yieldStarSyncIterableInvalidThenable.ts(12,12): error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member.
yieldStarSyncIterableInvalidThenable.ts(13,12): error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member.
yieldStarSyncIterableInvalidThenable.ts(14,31): error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member.
yieldStarSyncIterableInvalidThenable.ts(15,31): error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member.


==== yieldStarSyncIterableInvalidThenable.ts (8 errors) ====
declare const thenable: { then(onfulfilled: number): void };
declare const iterable: Iterable<number, typeof thenable>;
declare const union: number[] | Iterable<typeof thenable>;
declare const asyncIterable: AsyncIterable<typeof thenable>;

async function* invalid() {
yield* [thenable];
~~~~~~~~~~
!!! error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member.
yield thenable;
~~~~~~~~
!!! error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member.
for await (const value of [thenable]) {}
~~~~~~~~~~
!!! error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member.

yield* iterable;
~~~~~~~~
!!! error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member.
yield* iterable;
~~~~~~~~
!!! error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member.
yield* union;
~~~~~
!!! error TS1322: Type of iterated elements of a 'yield*' operand must either be a valid promise or must not contain a callable 'then' member.
for await (const value of union) {}
~~~~~
!!! error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member.
for await (const value of asyncIterable) {}
~~~~~~~~~~~~~
!!! error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member.
}

async function* valid() {
yield* [1];
yield* [Promise.resolve(1)];
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//// [tests/cases/compiler/yieldStarSyncIterableInvalidThenable.ts] ////

//// [yieldStarSyncIterableInvalidThenable.ts]
declare const thenable: { then(onfulfilled: number): void };
declare const iterable: Iterable<number, typeof thenable>;
declare const union: number[] | Iterable<typeof thenable>;
declare const asyncIterable: AsyncIterable<typeof thenable>;

async function* invalid() {
yield* [thenable];
yield thenable;
for await (const value of [thenable]) {}

yield* iterable;
yield* iterable;
yield* union;
for await (const value of union) {}
for await (const value of asyncIterable) {}
}

async function* valid() {
yield* [1];
yield* [Promise.resolve(1)];
}


//// [yieldStarSyncIterableInvalidThenable.js]
"use strict";
async function* invalid() {
yield* [thenable];
yield thenable;
for await (const value of [thenable]) { }
yield* iterable;
yield* iterable;
yield* union;
for await (const value of union) { }
for await (const value of asyncIterable) { }
}
async function* valid() {
yield* [1];
yield* [Promise.resolve(1)];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//// [tests/cases/compiler/yieldStarSyncIterableInvalidThenable.ts] ////

=== yieldStarSyncIterableInvalidThenable.ts ===
declare const thenable: { then(onfulfilled: number): void };
>thenable : Symbol(thenable, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 13))
>then : Symbol(then, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 25))
>onfulfilled : Symbol(onfulfilled, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 31))

declare const iterable: Iterable<number, typeof thenable>;
>iterable : Symbol(iterable, Decl(yieldStarSyncIterableInvalidThenable.ts, 1, 13))
>Iterable : Symbol(Iterable, Decl(lib.es2015.iterable.d.ts, --, --))
>thenable : Symbol(thenable, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 13))

declare const union: number[] | Iterable<typeof thenable>;
>union : Symbol(union, Decl(yieldStarSyncIterableInvalidThenable.ts, 2, 13))
>Iterable : Symbol(Iterable, Decl(lib.es2015.iterable.d.ts, --, --))
>thenable : Symbol(thenable, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 13))

declare const asyncIterable: AsyncIterable<typeof thenable>;
>asyncIterable : Symbol(asyncIterable, Decl(yieldStarSyncIterableInvalidThenable.ts, 3, 13))
>AsyncIterable : Symbol(AsyncIterable, Decl(lib.es2018.asynciterable.d.ts, --, --))
>thenable : Symbol(thenable, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 13))

async function* invalid() {
>invalid : Symbol(invalid, Decl(yieldStarSyncIterableInvalidThenable.ts, 3, 60))

yield* [thenable];
>thenable : Symbol(thenable, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 13))

yield thenable;
>thenable : Symbol(thenable, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 13))

for await (const value of [thenable]) {}
>value : Symbol(value, Decl(yieldStarSyncIterableInvalidThenable.ts, 8, 20))
>thenable : Symbol(thenable, Decl(yieldStarSyncIterableInvalidThenable.ts, 0, 13))

yield* iterable;
>iterable : Symbol(iterable, Decl(yieldStarSyncIterableInvalidThenable.ts, 1, 13))

yield* iterable;
>iterable : Symbol(iterable, Decl(yieldStarSyncIterableInvalidThenable.ts, 1, 13))

yield* union;
>union : Symbol(union, Decl(yieldStarSyncIterableInvalidThenable.ts, 2, 13))

for await (const value of union) {}
>value : Symbol(value, Decl(yieldStarSyncIterableInvalidThenable.ts, 13, 20))
>union : Symbol(union, Decl(yieldStarSyncIterableInvalidThenable.ts, 2, 13))

for await (const value of asyncIterable) {}
>value : Symbol(value, Decl(yieldStarSyncIterableInvalidThenable.ts, 14, 20))
>asyncIterable : Symbol(asyncIterable, Decl(yieldStarSyncIterableInvalidThenable.ts, 3, 13))
}

async function* valid() {
>valid : Symbol(valid, Decl(yieldStarSyncIterableInvalidThenable.ts, 15, 1))

yield* [1];
yield* [Promise.resolve(1)];
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//// [tests/cases/compiler/yieldStarSyncIterableInvalidThenable.ts] ////

=== yieldStarSyncIterableInvalidThenable.ts ===
declare const thenable: { then(onfulfilled: number): void };
>thenable : { then(onfulfilled: number): void; }
>then : (onfulfilled: number) => void
>onfulfilled : number

declare const iterable: Iterable<number, typeof thenable>;
>iterable : Iterable<number, { then(onfulfilled: number): void; }>
>thenable : { then(onfulfilled: number): void; }

declare const union: number[] | Iterable<typeof thenable>;
>union : number[] | Iterable<{ then(onfulfilled: number): void; }>
>thenable : { then(onfulfilled: number): void; }

declare const asyncIterable: AsyncIterable<typeof thenable>;
>asyncIterable : AsyncIterable<{ then(onfulfilled: number): void; }>
>thenable : { then(onfulfilled: number): void; }

async function* invalid() {
>invalid : () => AsyncGenerator<any, void, any>

yield* [thenable];
>yield* [thenable] : undefined
>[thenable] : { then(onfulfilled: number): void; }[]
>thenable : { then(onfulfilled: number): void; }

yield thenable;
>yield thenable : any
>thenable : { then(onfulfilled: number): void; }

for await (const value of [thenable]) {}
>value : any
>[thenable] : { then(onfulfilled: number): void; }[]
>thenable : { then(onfulfilled: number): void; }

yield* iterable;
>yield* iterable : any
>iterable : Iterable<number, { then(onfulfilled: number): void; }>

yield* iterable;
>yield* iterable : any
>iterable : Iterable<number, { then(onfulfilled: number): void; }>

yield* union;
>yield* union : any
>union : number[] | Iterable<{ then(onfulfilled: number): void; }>

for await (const value of union) {}
>value : any
>union : number[] | Iterable<{ then(onfulfilled: number): void; }>

for await (const value of asyncIterable) {}
>value : any
>asyncIterable : AsyncIterable<{ then(onfulfilled: number): void; }>
}

async function* valid() {
>valid : () => AsyncGenerator<number, void, unknown>

yield* [1];
>yield* [1] : undefined
>[1] : number[]
>1 : 1

yield* [Promise.resolve(1)];
>yield* [Promise.resolve(1)] : undefined
>[Promise.resolve(1)] : Promise<number>[]
>Promise.resolve(1) : Promise<number>
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T>(value: T | PromiseLike<T>): Promise<Awaited<T>>; }
>Promise : PromiseConstructor
>resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T>(value: T | PromiseLike<T>): Promise<Awaited<T>>; }
>1 : 1
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// @target: esnext
// @strict: true

declare const thenable: { then(onfulfilled: number): void };
declare const iterable: Iterable<number, typeof thenable>;
declare const union: number[] | Iterable<typeof thenable>;
declare const asyncIterable: AsyncIterable<typeof thenable>;

async function* invalid() {
yield* [thenable];
yield thenable;
for await (const value of [thenable]) {}

yield* iterable;
yield* iterable;
yield* union;
for await (const value of union) {}
for await (const value of asyncIterable) {}
}

async function* valid() {
yield* [1];
yield* [Promise.resolve(1)];
}