diff --git a/example/src/enums.ts b/example/src/enums.ts index 9b24dc77c..65451c0c1 100644 --- a/example/src/enums.ts +++ b/example/src/enums.ts @@ -51,3 +51,27 @@ export const EnumLikeObject = { Completed: "completed", } as const; + +/** + * Since TypeScript's `enum` can be inconvenient to work with, some packages define their own enum-like objects: + * + * ``` + * export const EnumLikeObjectNumValues = { + * Pending: 1, + * InProgress: 2, + * Completed: 3 + * } as const + * ``` + * + * Use the `@enum` tag to make TypeDoc document this object as an enum. + * + * @enum + */ +export const EnumLikeObjectNumValues = { + Pending: 1, + + /** Indicates that a courier is en route delivering this order. */ + InProgress: 2, + + Completed: 3, +} as const; diff --git a/src/lib/converter/symbols.ts b/src/lib/converter/symbols.ts index 348a34722..595fb9bce 100644 --- a/src/lib/converter/symbols.ts +++ b/src/lib/converter/symbols.ts @@ -881,7 +881,7 @@ function isEnumLike(checker: ts.TypeChecker, type: ts.Type, location: ts.Node) { return type.getProperties().every((prop) => { const propType = checker.getTypeOfSymbolAtLocation(prop, location); - return propType.isStringLiteral(); + return propType.isStringLiteral() || propType.isNumberLiteral(); }); } @@ -912,7 +912,7 @@ function convertVariableAsEnum( prop, declaration ); - assert(propType.isStringLiteral()); + assert(propType.isStringLiteral() || propType.isNumberLiteral()); reflection.defaultValue = JSON.stringify(propType.value); diff --git a/src/test/converter2/behavior/asConstEnum.ts b/src/test/converter2/behavior/asConstEnum.ts index dd215b5c8..4cbaa156d 100644 --- a/src/test/converter2/behavior/asConstEnum.ts +++ b/src/test/converter2/behavior/asConstEnum.ts @@ -1,3 +1,5 @@ +/* Enum-like objects with string literal values */ + export const SomeEnumLike = { a: "a", b: "b", @@ -23,3 +25,31 @@ export const ManualEnumHelper: Readonly<{ a: "a" }> = { export const WithoutReadonly = { a: "a", } as { a: "a" }; + +/* Enum-like objects with numeric literal values */ + +export const SomeEnumLikeNumeric = { + a: 0, + b: 1, +} as const; + +/** @enum */ +export const SomeEnumLikeTaggedNumeric = { + a: 0, + b: 1, +} as const; + +/** @enum */ +export const ManualEnumNumeric: { readonly a: 0 } = { + a: 0, +}; + +/** @enum */ +export const ManualEnumHelperNumeric: Readonly<{ a: 0 }> = { + a: 0, +}; + +/** @enum */ +export const WithoutReadonlyNumeric = { + a: 0, +} as { a: 0 };