|
1 | 1 | import ts from "typescript";
|
2 |
| -import type { Context, NodeParser } from "../NodeParser.js"; |
| 2 | +import { Context, NodeParser } from "../NodeParser.js"; |
3 | 3 | import type { SubNodeParser } from "../SubNodeParser.js";
|
4 | 4 | import { AliasType } from "../Type/AliasType.js";
|
5 | 5 | import type { BaseType } from "../Type/BaseType.js";
|
@@ -31,33 +31,41 @@ export class PromiseNodeParser implements SubNodeParser {
|
31 | 31 |
|
32 | 32 | const type = this.typeChecker.getTypeAtLocation(node);
|
33 | 33 |
|
34 |
| - // @ts-expect-error - Internal API of TypeScript |
35 | 34 | const awaitedType = this.typeChecker.getAwaitedType(type);
|
36 | 35 |
|
| 36 | + // ignores non awaitable types |
| 37 | + if (!awaitedType) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
37 | 41 | // If the awaited type differs from the original type, the type extends promise
|
38 | 42 | // Awaited<Promise<T>> -> T (Promise<T> !== T)
|
39 | 43 | // Awaited<Y> -> Y (Y === Y)
|
40 |
| - return awaitedType !== type && !this.typeChecker.isTypeAssignableTo(type, awaitedType); |
| 44 | + if (awaitedType === type) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + // In types like: A<T> = T, type C = A<1>, C has the same type as A<1> and 1, |
| 49 | + // the awaitedType is NOT the same reference as the type, so a assignability |
| 50 | + // check is needed |
| 51 | + return !this.typeChecker.isTypeAssignableTo(type, awaitedType); |
41 | 52 | }
|
42 | 53 |
|
43 | 54 | public createType(
|
44 | 55 | node: ts.InterfaceDeclaration | ts.ClassDeclaration | ts.ExpressionWithTypeArguments | ts.TypeAliasDeclaration,
|
45 | 56 | context: Context,
|
46 | 57 | ): BaseType {
|
47 | 58 | const type = this.typeChecker.getTypeAtLocation(node);
|
48 |
| - |
49 |
| - // @ts-expect-error - Internal API of TypeScript |
50 |
| - const awaitedType = this.typeChecker.getAwaitedType(type); |
51 |
| - |
52 |
| - const awaitedNode = this.typeChecker.typeToTypeNode(awaitedType, undefined, ts.NodeBuilderFlags.NoTruncation); |
| 59 | + const awaitedType = this.typeChecker.getAwaitedType(type)!; // supportsNode ensures this |
| 60 | + const awaitedNode = this.typeChecker.typeToTypeNode(awaitedType, undefined, ts.NodeBuilderFlags.IgnoreErrors); |
53 | 61 |
|
54 | 62 | if (!awaitedNode) {
|
55 | 63 | throw new Error(
|
56 | 64 | `Could not find awaited node for type ${node.pos === -1 ? "<unresolved>" : node.getText()}`,
|
57 | 65 | );
|
58 | 66 | }
|
59 | 67 |
|
60 |
| - const baseNode = this.childNodeParser.createType(awaitedNode, context); |
| 68 | + const baseNode = this.childNodeParser.createType(awaitedNode, new Context(node)); |
61 | 69 |
|
62 | 70 | const name = this.getNodeName(node);
|
63 | 71 |
|
|
0 commit comments