-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Report error for invalid 'this' type during 'await' #48946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
tests/baselines/reference/await_incorrectThisType.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
tests/cases/conformance/async/es2017/await_incorrectThisType.ts(40,1): error TS2684: The 'this' context of type 'EPromise<number, string>' is not assignable to method's 'this' of type 'EPromise<never, string>'. | ||
Type 'number' is not assignable to type 'never'. | ||
tests/cases/conformance/async/es2017/await_incorrectThisType.ts(43,5): error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member. | ||
The 'this' context of type 'EPromise<number, string>' is not assignable to method's 'this' of type 'EPromise<never, string>'. | ||
|
||
|
||
==== tests/cases/conformance/async/es2017/await_incorrectThisType.ts (2 errors) ==== | ||
// https://github.com/microsoft/TypeScript/issues/47711 | ||
type Either<E, A> = Left<E> | Right<A>; | ||
type Left<E> = { tag: 'Left', e: E }; | ||
type Right<A> = { tag: 'Right', a: A }; | ||
|
||
const mkLeft = <E>(e: E): Either<E, never> => ({ tag: 'Left', e }); | ||
const mkRight = <A>(a: A): Either<never, A> => ({ tag: 'Right', a }); | ||
|
||
class EPromise<E, A> implements PromiseLike<A> { | ||
static succeed<A>(a: A): EPromise<never, A> { | ||
return new EPromise(Promise.resolve(mkRight(a))); | ||
} | ||
|
||
static fail<E>(e: E): EPromise<E, never> { | ||
return new EPromise(Promise.resolve(mkLeft(e))); | ||
} | ||
|
||
constructor(readonly p: PromiseLike<Either<E, A>>) { } | ||
|
||
then<B = A, B1 = never>( | ||
// EPromise can act as a Thenable only when `E` is `never`. | ||
this: EPromise<never, A>, | ||
onfulfilled?: ((value: A) => B | PromiseLike<B>) | null | undefined, | ||
onrejected?: ((reason: any) => B1 | PromiseLike<B1>) | null | undefined | ||
): PromiseLike<B | B1> { | ||
return this.p.then( | ||
// Casting to `Right<A>` is safe here because we've eliminated the possibility of `Left<E>`. | ||
either => onfulfilled?.((either as Right<A>).a) ?? (either as Right<A>).a as unknown as B, | ||
onrejected | ||
) | ||
} | ||
} | ||
|
||
const withTypedFailure: EPromise<number, string> = EPromise.fail(1); | ||
|
||
// Errors as expected: | ||
// | ||
// "The 'this' context of type 'EPromise<number, string>' is not assignable to method's | ||
// 'this' of type 'EPromise<never, string>" | ||
withTypedFailure.then(s => s.toUpperCase()).then(console.log); | ||
~~~~~~~~~~~~~~~~ | ||
!!! error TS2684: The 'this' context of type 'EPromise<number, string>' is not assignable to method's 'this' of type 'EPromise<never, string>'. | ||
!!! error TS2684: Type 'number' is not assignable to type 'never'. | ||
|
||
async function test() { | ||
await withTypedFailure; | ||
~~~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member. | ||
!!! error TS1320: The 'this' context of type 'EPromise<number, string>' is not assignable to method's 'this' of type 'EPromise<never, string>'. | ||
} |
48 changes: 48 additions & 0 deletions
48
tests/cases/conformance/async/es2017/await_incorrectThisType.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// @target: esnext | ||
// @noEmit: true | ||
// @noTypesAndSymbols: true | ||
|
||
// https://github.com/microsoft/TypeScript/issues/47711 | ||
type Either<E, A> = Left<E> | Right<A>; | ||
type Left<E> = { tag: 'Left', e: E }; | ||
type Right<A> = { tag: 'Right', a: A }; | ||
|
||
const mkLeft = <E>(e: E): Either<E, never> => ({ tag: 'Left', e }); | ||
const mkRight = <A>(a: A): Either<never, A> => ({ tag: 'Right', a }); | ||
|
||
class EPromise<E, A> implements PromiseLike<A> { | ||
static succeed<A>(a: A): EPromise<never, A> { | ||
return new EPromise(Promise.resolve(mkRight(a))); | ||
} | ||
|
||
static fail<E>(e: E): EPromise<E, never> { | ||
return new EPromise(Promise.resolve(mkLeft(e))); | ||
} | ||
|
||
constructor(readonly p: PromiseLike<Either<E, A>>) { } | ||
|
||
then<B = A, B1 = never>( | ||
// EPromise can act as a Thenable only when `E` is `never`. | ||
this: EPromise<never, A>, | ||
onfulfilled?: ((value: A) => B | PromiseLike<B>) | null | undefined, | ||
onrejected?: ((reason: any) => B1 | PromiseLike<B1>) | null | undefined | ||
): PromiseLike<B | B1> { | ||
return this.p.then( | ||
// Casting to `Right<A>` is safe here because we've eliminated the possibility of `Left<E>`. | ||
either => onfulfilled?.((either as Right<A>).a) ?? (either as Right<A>).a as unknown as B, | ||
onrejected | ||
) | ||
} | ||
} | ||
|
||
const withTypedFailure: EPromise<number, string> = EPromise.fail(1); | ||
|
||
// Errors as expected: | ||
// | ||
// "The 'this' context of type 'EPromise<number, string>' is not assignable to method's | ||
// 'this' of type 'EPromise<never, string>" | ||
withTypedFailure.then(s => s.toUpperCase()).then(console.log); | ||
|
||
async function test() { | ||
await withTypedFailure; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably should be a union since you don't want to issue the error twice, and the only place
thisTypeForErrorOut
is added did not previously passerrorNode
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to have to test whether
errorNode
is aNode
and I'd also rather not allocate a nursery object for{ errorNode }
if I don't have to.