Skip to content

Commit

Permalink
feat: add 1058-1062
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Sep 10, 2023
1 parent ec8b012 commit 6ea7585
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/1058.md
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)
16 changes: 16 additions & 0 deletions docs/1058_fix_01.md
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() {},
};
}
}
```
10 changes: 10 additions & 0 deletions docs/1059.md
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.
7 changes: 7 additions & 0 deletions docs/1059_fix_01.md
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.
7 changes: 7 additions & 0 deletions docs/1060.md
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.
8 changes: 8 additions & 0 deletions docs/1060_fix_01.md
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.
13 changes: 13 additions & 0 deletions docs/1061.md
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,
}
```
12 changes: 12 additions & 0 deletions docs/1061_fix_01.md
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",
}
```
19 changes: 19 additions & 0 deletions docs/1062.md
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();
}
```
5 changes: 5 additions & 0 deletions docs/1062_fix_01.md
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.

0 comments on commit 6ea7585

Please sign in to comment.