Skip to content

Commit 3f0fc08

Browse files
committed
fix types in getTSType
1 parent 7331723 commit 3f0fc08

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

packages/react-docgen/src/utils/getTSType.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ import type {
3636
RestElement,
3737
TypeScript,
3838
TSQualifiedName,
39+
TSLiteralType,
3940
} from '@babel/types';
4041
import { getDocblock } from './docblock.js';
4142

42-
const tsTypes = {
43+
const tsTypes: Record<string, string> = {
4344
TSAnyKeyword: 'any',
4445
TSBooleanKeyword: 'boolean',
4546
TSUnknownKeyword: 'unknown',
@@ -54,7 +55,13 @@ const tsTypes = {
5455
TSVoidKeyword: 'void',
5556
};
5657

57-
const namedTypes = {
58+
const namedTypes: Record<
59+
string,
60+
(
61+
path: NodePath<any>,
62+
typeParams: TypeParameters | null,
63+
) => TypeDescriptor | null
64+
> = {
5865
TSArrayType: handleTSArrayType,
5966
TSTypeReference: handleTSTypeReference,
6067
TSTypeLiteral: handleTSTypeLiteral,
@@ -67,6 +74,7 @@ const namedTypes = {
6774
TSTypeQuery: handleTSTypeQuery,
6875
TSTypeOperator: handleTSTypeOperator,
6976
TSIndexedAccessType: handleTSIndexedAccessType,
77+
TSLiteralType: handleTSLiteralType,
7078
};
7179

7280
function handleTSQualifiedName(
@@ -85,6 +93,15 @@ function handleTSQualifiedName(
8593
return { name: printValue(path).replace(/<.*>$/, '') };
8694
}
8795

96+
function handleTSLiteralType(path: NodePath<TSLiteralType>): LiteralType {
97+
const literal = path.get('literal');
98+
99+
return {
100+
name: 'literal',
101+
value: printValue(literal),
102+
};
103+
}
104+
88105
function handleTSArrayType(
89106
path: NodePath<TSArrayType>,
90107
typeParams: TypeParameters | null,
@@ -98,7 +115,7 @@ function handleTSArrayType(
98115

99116
function handleTSTypeReference(
100117
path: NodePath<TSTypeReference>,
101-
typeParams: TypeParameters,
118+
typeParams: TypeParameters | null,
102119
): TypeDescriptor<TSFunctionSignatureType> | null {
103120
let type: TypeDescriptor<TSFunctionSignatureType>;
104121
const typeName = path.get('typeName');
@@ -508,16 +525,9 @@ function getTSTypeWithResolvedTypes(
508525
}
509526

510527
if (node.type in tsTypes) {
511-
type = { name: tsTypes[node.type] };
512-
} else if (path.isTSLiteralType()) {
513-
const literal = path.get('literal');
514-
515-
type = {
516-
name: 'literal',
517-
value: printValue(literal),
518-
};
528+
type = { name: tsTypes[node.type]! };
519529
} else if (node.type in namedTypes) {
520-
type = namedTypes[node.type](path, typeParams);
530+
type = namedTypes[node.type]!(path, typeParams);
521531
}
522532

523533
if (!type) {

0 commit comments

Comments
 (0)