Skip to content

Commit 697805f

Browse files
expose internals
1 parent 7944bc7 commit 697805f

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"@types/jest": "^29.5.12",
6666
"@types/node": "^20.12.7",
6767
"@types/normalize-path": "^3.0.2",
68+
"@types/ts-expose-internals": "npm:ts-expose-internals@^5.4.5",
6869
"ajv": "^8.12.0",
6970
"ajv-formats": "^3.0.1",
7071
"auto": "^11.1.6",

src/NodeParser/FunctionNodeParser.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ export class FunctionNodeParser implements SubNodeParser {
1616
) {}
1717

1818
public supportsNode(node: ts.TypeNode): boolean {
19-
return (
20-
node.kind === ts.SyntaxKind.FunctionType ||
21-
node.kind === ts.SyntaxKind.FunctionExpression ||
22-
node.kind === ts.SyntaxKind.ArrowFunction ||
23-
node.kind === ts.SyntaxKind.FunctionDeclaration
24-
);
19+
return node.kind === ts.SyntaxKind.FunctionType;
2520
}
2621

2722
public createType(

src/NodeParser/PromiseNodeParser.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ts from "typescript";
2-
import type { Context, NodeParser } from "../NodeParser.js";
2+
import { Context, NodeParser } from "../NodeParser.js";
33
import type { SubNodeParser } from "../SubNodeParser.js";
44
import { AliasType } from "../Type/AliasType.js";
55
import type { BaseType } from "../Type/BaseType.js";
@@ -31,33 +31,41 @@ export class PromiseNodeParser implements SubNodeParser {
3131

3232
const type = this.typeChecker.getTypeAtLocation(node);
3333

34-
// @ts-expect-error - Internal API of TypeScript
3534
const awaitedType = this.typeChecker.getAwaitedType(type);
3635

36+
// ignores non awaitable types
37+
if (!awaitedType) {
38+
return false;
39+
}
40+
3741
// If the awaited type differs from the original type, the type extends promise
3842
// Awaited<Promise<T>> -> T (Promise<T> !== T)
3943
// 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);
4152
}
4253

4354
public createType(
4455
node: ts.InterfaceDeclaration | ts.ClassDeclaration | ts.ExpressionWithTypeArguments | ts.TypeAliasDeclaration,
4556
context: Context,
4657
): BaseType {
4758
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);
5361

5462
if (!awaitedNode) {
5563
throw new Error(
5664
`Could not find awaited node for type ${node.pos === -1 ? "<unresolved>" : node.getText()}`,
5765
);
5866
}
5967

60-
const baseNode = this.childNodeParser.createType(awaitedNode, context);
68+
const baseNode = this.childNodeParser.createType(awaitedNode, new Context(node));
6169

6270
const name = this.getNodeName(node);
6371

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,11 @@
19711971
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.3.tgz#6209321eb2c1712a7e7466422b8cb1fc0d9dd5d8"
19721972
integrity sha512-9aEbYZ3TbYMznPdcdr3SmIrLXwC/AKZXQeCf9Pgao5CKb8CyHuEX5jzWPTkvregvhRJHcpRO6BFoGW9ycaOkYw==
19731973

1974+
"@types/ts-expose-internals@npm:ts-expose-internals@^5.4.5":
1975+
version "5.4.5"
1976+
resolved "https://registry.yarnpkg.com/ts-expose-internals/-/ts-expose-internals-5.4.5.tgz#94da2b665627135ad1281d98af3ccb08cb4c1950"
1977+
integrity sha512-0HfRwjgSIOyuDlHzkFedMWU4aHWq9pu4MUKHgH75U+L76wCAtK5WB0rc/dAIhulMRcPUlcKONeiiR5Sxy/7XcA==
1978+
19741979
"@types/yargs-parser@*":
19751980
version "21.0.3"
19761981
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.3.tgz#815e30b786d2e8f0dcd85fd5bcf5e1a04d008f15"

0 commit comments

Comments
 (0)