Skip to content

Commit 8b34045

Browse files
committed
Support enum-like objects with numeric literal values
TypeDoc already supports enum-like objects with string literal values via TypeStrong#1675 and TypeStrong#1740. Enum-like objects with numeric literal values should also be supported, as they are a popular choice for developers wishing to avoid use of TypeScript enums. This PR adds support for such objects. Tests and a new example have been added to cover this type of object. Resolves TypeStrong#1918.
1 parent abed9a6 commit 8b34045

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

example/src/enums.ts

+24
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,27 @@ export const EnumLikeObject = {
5151

5252
Completed: "completed",
5353
} as const;
54+
55+
/**
56+
* Since TypeScript's `enum` can be inconvenient to work with, some packages define their own enum-like objects:
57+
*
58+
* ```
59+
* export const EnumLikeObjectNumValues = {
60+
* Pending: 1,
61+
* InProgress: 2,
62+
* Completed: 3
63+
* } as const
64+
* ```
65+
*
66+
* Use the `@enum` tag to make TypeDoc document this object as an enum.
67+
*
68+
* @enum
69+
*/
70+
export const EnumLikeObjectNumValues = {
71+
Pending: 1,
72+
73+
/** Indicates that a courier is en route delivering this order. */
74+
InProgress: 2,
75+
76+
Completed: 3,
77+
} as const;

src/lib/converter/symbols.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ function isEnumLike(checker: ts.TypeChecker, type: ts.Type, location: ts.Node) {
881881

882882
return type.getProperties().every((prop) => {
883883
const propType = checker.getTypeOfSymbolAtLocation(prop, location);
884-
return propType.isStringLiteral();
884+
return propType.isStringLiteral() || propType.isNumberLiteral();
885885
});
886886
}
887887

@@ -912,7 +912,7 @@ function convertVariableAsEnum(
912912
prop,
913913
declaration
914914
);
915-
assert(propType.isStringLiteral());
915+
assert(propType.isStringLiteral() || propType.isNumberLiteral());
916916

917917
reflection.defaultValue = JSON.stringify(propType.value);
918918

src/test/converter2/behavior/asConstEnum.ts

+30
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* Enum-like objects with string literal values */
2+
13
export const SomeEnumLike = {
24
a: "a",
35
b: "b",
@@ -23,3 +25,31 @@ export const ManualEnumHelper: Readonly<{ a: "a" }> = {
2325
export const WithoutReadonly = {
2426
a: "a",
2527
} as { a: "a" };
28+
29+
/* Enum-like objects with numeric literal values */
30+
31+
export const SomeEnumLikeNumeric = {
32+
a: 0,
33+
b: 1,
34+
} as const;
35+
36+
/** @enum */
37+
export const SomeEnumLikeTaggedNumeric = {
38+
a: 0,
39+
b: 1,
40+
} as const;
41+
42+
/** @enum */
43+
export const ManualEnumNumeric: { readonly a: 0 } = {
44+
a: 0,
45+
};
46+
47+
/** @enum */
48+
export const ManualEnumHelperNumeric: Readonly<{ a: 0 }> = {
49+
a: 0,
50+
};
51+
52+
/** @enum */
53+
export const WithoutReadonlyNumeric = {
54+
a: 0,
55+
} as { a: 0 };

0 commit comments

Comments
 (0)