Skip to content

Commit 2e074a7

Browse files
committed
fix(compiler-sfc): infer correct type for enums
fix #7511
1 parent 0002567 commit 2e074a7

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,8 @@ interface Test {}
16771677

16781678
type Alias = number[]
16791679

1680+
enum Enum { one = '1', two = '2' }
1681+
16801682

16811683
export default /*#__PURE__*/_defineComponent({
16821684
props: {
@@ -1702,6 +1704,7 @@ export default /*#__PURE__*/_defineComponent({
17021704
symbol: { type: Symbol, required: true },
17031705
extract: { type: Number, required: true },
17041706
exclude: { type: [Number, Boolean], required: true },
1707+
enum: { type: Object, required: true },
17051708
uppercase: { type: String, required: true },
17061709
params: { type: Array, required: true },
17071710
nonNull: { type: String, required: true },
@@ -1719,7 +1722,7 @@ export default /*#__PURE__*/_defineComponent({
17191722

17201723

17211724

1722-
return { }
1725+
return { Enum }
17231726
}
17241727

17251728
})"

packages/compiler-sfc/__tests__/compileScript.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,8 @@ const emit = defineEmits(['a', 'b'])
998998
999999
type Alias = number[]
10001000
1001+
enum Enum { one = '1', two = '2' }
1002+
10011003
defineProps<{
10021004
string: string
10031005
number: number
@@ -1021,6 +1023,7 @@ const emit = defineEmits(['a', 'b'])
10211023
symbol: symbol
10221024
extract: Extract<1 | 2 | boolean, 2>
10231025
exclude: Exclude<1 | 2 | boolean, 2>
1026+
enum: Enum
10241027
uppercase: Uppercase<'foo'>
10251028
params: Parameters<(foo: any) => void>
10261029
nonNull: NonNullable<string | null>
@@ -1066,6 +1069,7 @@ const emit = defineEmits(['a', 'b'])
10661069
expect(content).toMatch(
10671070
`exclude: { type: [Number, Boolean], required: true }`
10681071
)
1072+
expect(content).toMatch(`enum: { type: Object, required: true }`)
10691073
expect(content).toMatch(`uppercase: { type: String, required: true }`)
10701074
expect(content).toMatch(`params: { type: Array, required: true }`)
10711075
expect(content).toMatch(`nonNull: { type: String, required: true }`)
@@ -1115,7 +1119,9 @@ const emit = defineEmits(['a', 'b'])
11151119
foo: BindingTypes.PROPS,
11161120
uppercase: BindingTypes.PROPS,
11171121
params: BindingTypes.PROPS,
1118-
nonNull: BindingTypes.PROPS
1122+
nonNull: BindingTypes.PROPS,
1123+
enum: BindingTypes.PROPS,
1124+
Enum: BindingTypes.LITERAL_CONST
11191125
})
11201126
})
11211127

packages/compiler-sfc/src/compileScript.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,14 +1369,15 @@ export function compileScript(
13691369
if (isTS) {
13701370
// move all Type declarations to outer scope
13711371
if (
1372-
(node.type.startsWith('TS') ||
1373-
(node.type === 'ExportNamedDeclaration' &&
1374-
node.exportKind === 'type') ||
1375-
(node.type === 'VariableDeclaration' && node.declare)) &&
1376-
node.type !== 'TSEnumDeclaration'
1372+
node.type.startsWith('TS') ||
1373+
(node.type === 'ExportNamedDeclaration' &&
1374+
node.exportKind === 'type') ||
1375+
(node.type === 'VariableDeclaration' && node.declare)
13771376
) {
13781377
recordType(node, declaredTypes)
1379-
hoistNode(node)
1378+
if (node.type !== 'TSEnumDeclaration') {
1379+
hoistNode(node)
1380+
}
13801381
}
13811382
}
13821383
}
@@ -1957,7 +1958,10 @@ interface PropTypeData {
19571958
}
19581959

19591960
function recordType(node: Node, declaredTypes: Record<string, string[]>) {
1960-
if (node.type === 'TSInterfaceDeclaration') {
1961+
if (
1962+
node.type === 'TSInterfaceDeclaration' ||
1963+
node.type === 'TSEnumDeclaration'
1964+
) {
19611965
declaredTypes[node.id.name] = [`Object`]
19621966
} else if (node.type === 'TSTypeAliasDeclaration') {
19631967
declaredTypes[node.id.name] = inferRuntimeType(

0 commit comments

Comments
 (0)