Skip to content

Commit b44377a

Browse files
committed
Resolve generic constraints like other types
1 parent 2117fb7 commit b44377a

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

src/models/types/typeParameter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class TypeParameterNode implements TypeNode {
66
constructor(
77
public name: string,
88
public parentNamespaces: string[],
9-
public constraint: string | undefined,
9+
public constraint: TypeNode | undefined,
1010
public defaultValue: TypeNode | undefined,
1111
) {}
1212
}

src/parsers/typeResolver.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,14 @@ export function resolveType(
7575
try {
7676
if (type.flags & ts.TypeFlags.TypeParameter && type.symbol) {
7777
const declaration = type.symbol.declarations?.[0] as ts.TypeParameterDeclaration | undefined;
78+
const constraintType = declaration?.constraint
79+
? checker.getBaseConstraintOfType(type)
80+
: undefined;
81+
7882
return new TypeParameterNode(
7983
type.symbol.name,
8084
namespaces,
81-
declaration?.constraint?.getText(),
85+
constraintType ? resolveType(constraintType, undefined, context) : undefined,
8286
declaration?.default
8387
? resolveType(checker.getTypeAtLocation(declaration.default), undefined, context)
8488
: undefined,

test/conditional-types/output.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
"kind": "typeParameter",
1616
"name": "T",
1717
"parentNamespaces": [],
18-
"constraint": "boolean"
18+
"constraint": {
19+
"kind": "intrinsic",
20+
"parentNamespaces": [],
21+
"intrinsic": "boolean"
22+
}
1923
},
2024
"name": "x",
2125
"optional": false

test/generics/output.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@
2222
"kind": "typeParameter",
2323
"name": "T",
2424
"parentNamespaces": [],
25-
"constraint": "React.HTMLAttributes<HTMLElement>"
25+
"constraint": {
26+
"kind": "reference",
27+
"name": "HTMLAttributes<HTMLElement>",
28+
"parentNamespaces": [
29+
"React"
30+
]
31+
}
2632
},
2733
"optional": false
2834
},
@@ -67,7 +73,13 @@
6773
"kind": "typeParameter",
6874
"name": "T",
6975
"parentNamespaces": [],
70-
"constraint": "React.HTMLAttributes<HTMLElement>"
76+
"constraint": {
77+
"kind": "reference",
78+
"name": "HTMLAttributes<HTMLElement>",
79+
"parentNamespaces": [
80+
"React"
81+
]
82+
}
7183
}
7284
}
7385
]

test/mui-overridable-component/output.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
"kind": "typeParameter",
1717
"name": "RootComponent",
1818
"parentNamespaces": [],
19-
"constraint": "React.ElementType"
19+
"constraint": {
20+
"kind": "reference",
21+
"name": "ElementType<any, keyof IntrinsicElements>",
22+
"parentNamespaces": [
23+
"React"
24+
]
25+
}
2026
},
2127
{
2228
"kind": "intrinsic",

0 commit comments

Comments
 (0)