Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for as const enums #1675

Closed
Gerrit0 opened this issue Sep 5, 2021 · 3 comments
Closed

Support for as const enums #1675

Gerrit0 opened this issue Sep 5, 2021 · 3 comments
Labels
enhancement Improved functionality

Comments

@Gerrit0
Copy link
Collaborator

Gerrit0 commented Sep 5, 2021

Search Terms

as const, enumerations

Problem

Since TypeScript's enum is rather inconvenient to work with, some packages define their own enum-like objects, TypeDoc does this too.

/**
* Defines how entry points are interpreted.
* @enum
*/
export const EntryPointStrategy = {
/**
* The default behavior in v0.22+, expects all provided entry points as being part of a single program.
* Any directories included in the entry point list will result in `dir/index.[tj]sx?` being used.
*/
Resolve: "resolve",
/**
* The default behavior in v0.21 and earlier. Behaves like the resolve behavior, but will recursively
* expand directories into an entry point for each file within the directory.
*/
Expand: "expand",
/**
* Alternative resolution mode useful for monorepos. With this mode, TypeDoc will look for a package.json
* and tsconfig.json under each provided entry point. The `main` field of each package will be documented.
*/
Packages: "packages",
} as const;

However, the generated documentation doesn't make it obvious that this value should be treated as an enumeration.

image
image

Suggested Solution

Detect variables annotated with @enum with all readonly properties that are string literals, and convert them as an enumeration.

If there is also a type alias by the same name whose content is typeof T[keyof typeof T], remove it from the documentation since enums are also types.

@Gerrit0 Gerrit0 added the enhancement Improved functionality label Sep 5, 2021
@Gerrit0
Copy link
Collaborator Author

Gerrit0 commented Sep 5, 2021

I'm going to look at this again before releasing 0.23, should TypeDoc do this even if you don't tag the variable with @enum?

Vote: 👍 👎

@Gerrit0 Gerrit0 added this to To Do in Version 0.22 via automation Sep 5, 2021
@Gerrit0 Gerrit0 moved this from To Do to Done in Version 0.22 Sep 5, 2021
@michkot
Copy link

michkot commented Oct 5, 2021

@Gerrit0 Could this be extended for enum-object declarations?

eg. instead of

/** @enum /
export const EntryPointStrategy = {
/
*
* abcd
*/
Resolve: "resolve",
} as const;

there is (in .d.ts ) just

/** @enum /
export ( declare ) const EntryPointStrategy: {
/
*
* abcd
*/
readonly Resolve: "resolve",
};

there is even a "convenience variant":

/** @enum /
export const EntryPointStrategy: Readonly<{
/
*
* abcd
*/
Resolve: "resolve",
}>;

I am happy to open new issue for this if this is doable!
(This is also especially needed when typedoc is executed after pre-processing by api-extractor)

@Gerrit0
Copy link
Collaborator Author

Gerrit0 commented Oct 7, 2021

The one without Readonly<> should definitely be possible. The read only one will be more annoying to detect, but probably possible too.

ejuda added a commit to ejuda/typedoc that referenced this issue Apr 12, 2022
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improved functionality
Projects
No open projects
Development

No branches or pull requests

2 participants