-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,4 +1,31 @@ | ||
--- | ||
title: "The return type of an async function must either be a valid promise or must not contain a callable 'then' member." | ||
category: error | ||
tags: | ||
- async | ||
- syntax-error | ||
- promises | ||
related: | ||
- 1059 | ||
- 1060 | ||
--- | ||
|
||
Async functions cannot return objects with a callable then property as | ||
Javascript aggressively flattens promises. Therefore the following is an error: | ||
|
||
```ts | ||
class A { | ||
async test() { | ||
return { | ||
then() {}, | ||
}; | ||
} | ||
} | ||
``` | ||
|
||
### See also | ||
|
||
- [StackOverflow - Typescript error when function return type is Promise<{ then: () => void }>](https://stackoverflow.com/questions/47111363/typescript-error-when-function-return-type-is-promise-then-void) | ||
- [StackOverflow - Fulfill (don't resolve) promise with another promise](https://stackoverflow.com/questions/32168194/fulfill-dont-resolve-promise-with-another-promise) | ||
- [Monads for the Rest of Us - Why Promises are not Monads](https://gist.github.com/fatcerberus/beae4d15842071eab24fca2f0740c2ef#monads-its-the-law) | ||
- [MDN - `Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) |
This file contains 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,16 @@ | ||
--- | ||
title: "Rename the method/property." | ||
--- | ||
|
||
The best fix for this error is to rename the `then` method on the object you are | ||
returning to something else. `chain` or `map` might be appropriate: | ||
|
||
```ts | ||
class A { | ||
async test() { | ||
return { | ||
chain() {}, | ||
}; | ||
} | ||
} | ||
``` |
This file contains 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 |
---|---|---|
@@ -1,4 +1,14 @@ | ||
--- | ||
title: "A promise must have a 'then' method." | ||
category: error | ||
tags: | ||
- promise | ||
- typescript-only | ||
- async | ||
related: | ||
- 1058 | ||
- 1060 | ||
--- | ||
|
||
This is caused by the global `Promise` not containing a callable `then` method | ||
and therefore not supporting `async`/`await` downlevel emit. |
This file contains 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,7 @@ | ||
--- | ||
title: "Issue with types." | ||
--- | ||
|
||
The most common fix for this is that is the types for the 3rd party promise | ||
library is not configured properly. Refer to current documentation for using the | ||
3rd party promise library. |
This file contains 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 |
---|---|---|
@@ -1,4 +1,11 @@ | ||
--- | ||
title: "The first parameter of the 'then' method of a promise must be a callback." | ||
category: error | ||
tags: | ||
- promise | ||
- typescript-only | ||
- async | ||
--- | ||
|
||
This is caused by the global `Promise` not having a compatible signature to | ||
support `async`/`await` downlevel emit. |
This file contains 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,8 @@ | ||
--- | ||
title: "Issue with promise library." | ||
--- | ||
|
||
Either the 3rd party promise library cannot be used with TypeScript to down | ||
level emit `async`/`await` or there is a configuration issue with the library | ||
that is not allowing TypeScript to understand it will work. Consult the current | ||
documentation for the 3rd party promise library. |
This file contains 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 |
---|---|---|
@@ -1,4 +1,17 @@ | ||
--- | ||
title: "Enum member must have initializer." | ||
category: error | ||
tags: | ||
- enums | ||
- typescript-only | ||
--- | ||
|
||
When an enum contains string members, TypeScript cannot infer the value of | ||
members. Therefore the following will error: | ||
|
||
```ts | ||
enum A { | ||
a = "1", | ||
b, | ||
} | ||
``` |
This file contains 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,12 @@ | ||
--- | ||
title: "Provide an initializer for each member." | ||
--- | ||
|
||
Explicitly initialize the member: | ||
|
||
```ts | ||
enum A { | ||
a = "1", | ||
b = "2", | ||
} | ||
``` |
This file contains 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 |
---|---|---|
@@ -1,4 +1,23 @@ | ||
--- | ||
title: "Type is referenced directly or indirectly in the fulfillment callback of its own 'then' method." | ||
category: error | ||
tags: | ||
- promise | ||
- async | ||
--- | ||
|
||
When implementing a non-native promise type, if the type references itself in | ||
the `then`` method, this error will occur: | ||
|
||
```ts | ||
declare class BadPromise { | ||
then( | ||
onfulfilled: (value: BadPromise) => any, | ||
onrejected: (error: any) => any, | ||
): BadPromise; | ||
} | ||
|
||
async function test() { | ||
await new BadPromise(); | ||
} | ||
``` |
This file contains 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,5 @@ | ||
--- | ||
title: "Don't use a recursive type." | ||
--- | ||
|
||
To fix the error, don't use a recursive type in the definition of the promise. |