diff --git a/packages/component-meta/lib/schemaResolvers.ts b/packages/component-meta/lib/schemaResolvers.ts index 01d8a33b7b..a545ac39c8 100644 --- a/packages/component-meta/lib/schemaResolvers.ts +++ b/packages/component-meta/lib/schemaResolvers.ts @@ -331,6 +331,19 @@ export function createSchemaResolvers( else if (subtype.getCallSignatures().length === 1) { return resolveCallbackSchema(subtype.getCallSignatures()[0]!); } + else if (subtype.flags & ts.TypeFlags.EnumLiteral && subtype.isLiteral()) { + // enum members stringify to their qualified name (e.g. "MyEnum.Small"), which + // hides the runtime value from consumers such as docs generators; expose the + // value alongside, printed like any other literal + const { value } = subtype; + if (typeof value === 'string' || typeof value === 'number') { + return { + kind: 'literal', + type, + value: JSON.stringify(value), + }; + } + } return type; } diff --git a/packages/component-meta/lib/types.ts b/packages/component-meta/lib/types.ts index 65a27e486a..7e527d5af3 100644 --- a/packages/component-meta/lib/types.ts +++ b/packages/component-meta/lib/types.ts @@ -101,6 +101,7 @@ export interface ExposeMeta { export type PropertyMetaSchema = | string + | { kind: 'literal'; type: string; value: string } | { kind: 'enum'; type: string; schema?: PropertyMetaSchema[] } | { kind: 'array'; type: string; schema?: PropertyMetaSchema[] } | { kind: 'event'; type: string; schema?: PropertyMetaSchema[] } diff --git a/packages/component-meta/tests/index.spec.ts b/packages/component-meta/tests/index.spec.ts index 9e7b29712a..93db4bac08 100644 --- a/packages/component-meta/tests/index.spec.ts +++ b/packages/component-meta/tests/index.spec.ts @@ -612,9 +612,21 @@ const worker = (checker: ComponentMetaChecker, withTsconfig: boolean) => "schema": { "kind": "enum", "schema": [ - "MyEnum.Small", - "MyEnum.Medium", - "MyEnum.Large", + { + "kind": "literal", + "type": "MyEnum.Small", + "value": "0", + }, + { + "kind": "literal", + "type": "MyEnum.Medium", + "value": "1", + }, + { + "kind": "literal", + "type": "MyEnum.Large", + "value": "2", + }, ], "type": "MyEnum", }, @@ -623,6 +635,44 @@ const worker = (checker: ComponentMetaChecker, withTsconfig: boolean) => } `); + const stringEnumValue = meta.props.find(prop => prop.name === 'stringEnumValue'); + expect(stringEnumValue).toMatchInlineSnapshot(` + { + "declarations": [], + "default": undefined, + "description": "string enum value", + "getDeclarations": [Function], + "getTypeObject": [Function], + "global": false, + "name": "stringEnumValue", + "rawType": undefined, + "required": true, + "schema": { + "kind": "enum", + "schema": [ + { + "kind": "literal", + "type": "MyStringEnum.Small", + "value": ""small"", + }, + { + "kind": "literal", + "type": "MyStringEnum.Medium", + "value": ""medium"", + }, + { + "kind": "literal", + "type": "MyStringEnum.Large", + "value": ""large"", + }, + ], + "type": "MyStringEnum", + }, + "tags": [], + "type": "MyStringEnum", + } + `); + const namespaceType = meta.props.find(prop => prop.name === 'namespaceType'); expect(namespaceType).toMatchInlineSnapshot(` { diff --git a/packages/tsc/tests/__snapshots__/dts.spec.ts.snap b/packages/tsc/tests/__snapshots__/dts.spec.ts.snap index 710a7cc031..0ebd2a6f9c 100644 --- a/packages/tsc/tests/__snapshots__/dts.spec.ts.snap +++ b/packages/tsc/tests/__snapshots__/dts.spec.ts.snap @@ -753,6 +753,11 @@ declare enum MyEnum { Medium = 1, Large = 2 } +declare enum MyStringEnum { + Small = "small", + Medium = "medium", + Large = "large" +} declare namespace MyNamespace { type MyType = {}; } @@ -818,6 +823,10 @@ export interface MyProps { * enum value */ enumValue: MyEnum; + /** + * string enum value + */ + stringEnumValue: MyStringEnum; /** * namespace type */ diff --git a/test-workspace/component-meta/reference-type-props/my-props.ts b/test-workspace/component-meta/reference-type-props/my-props.ts index daee9ab006..5f1dae4486 100644 --- a/test-workspace/component-meta/reference-type-props/my-props.ts +++ b/test-workspace/component-meta/reference-type-props/my-props.ts @@ -19,6 +19,12 @@ enum MyEnum { Large, } +enum MyStringEnum { + Small = 'small', + Medium = 'medium', + Large = 'large', +} + namespace MyNamespace { export type MyType = {}; } @@ -94,6 +100,10 @@ export interface MyProps { * enum value */ enumValue: MyEnum; + /** + * string enum value + */ + stringEnumValue: MyStringEnum; /** * namespace type */