Skip to content

Commit

Permalink
fix(compiler-sfc): infer correct type for enums
Browse files Browse the repository at this point in the history
fix #7511
  • Loading branch information
yyx990803 committed Mar 28, 2023
1 parent 0002567 commit 2e074a7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,8 @@ interface Test {}

type Alias = number[]

enum Enum { one = '1', two = '2' }


export default /*#__PURE__*/_defineComponent({
props: {
Expand All @@ -1702,6 +1704,7 @@ export default /*#__PURE__*/_defineComponent({
symbol: { type: Symbol, required: true },
extract: { type: Number, required: true },
exclude: { type: [Number, Boolean], required: true },
enum: { type: Object, required: true },
uppercase: { type: String, required: true },
params: { type: Array, required: true },
nonNull: { type: String, required: true },
Expand All @@ -1719,7 +1722,7 @@ export default /*#__PURE__*/_defineComponent({



return { }
return { Enum }
}

})"
Expand Down
8 changes: 7 additions & 1 deletion packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,8 @@ const emit = defineEmits(['a', 'b'])
type Alias = number[]
enum Enum { one = '1', two = '2' }
defineProps<{
string: string
number: number
Expand All @@ -1021,6 +1023,7 @@ const emit = defineEmits(['a', 'b'])
symbol: symbol
extract: Extract<1 | 2 | boolean, 2>
exclude: Exclude<1 | 2 | boolean, 2>
enum: Enum
uppercase: Uppercase<'foo'>
params: Parameters<(foo: any) => void>
nonNull: NonNullable<string | null>
Expand Down Expand Up @@ -1066,6 +1069,7 @@ const emit = defineEmits(['a', 'b'])
expect(content).toMatch(
`exclude: { type: [Number, Boolean], required: true }`
)
expect(content).toMatch(`enum: { type: Object, required: true }`)
expect(content).toMatch(`uppercase: { type: String, required: true }`)
expect(content).toMatch(`params: { type: Array, required: true }`)
expect(content).toMatch(`nonNull: { type: String, required: true }`)
Expand Down Expand Up @@ -1115,7 +1119,9 @@ const emit = defineEmits(['a', 'b'])
foo: BindingTypes.PROPS,
uppercase: BindingTypes.PROPS,
params: BindingTypes.PROPS,
nonNull: BindingTypes.PROPS
nonNull: BindingTypes.PROPS,
enum: BindingTypes.PROPS,
Enum: BindingTypes.LITERAL_CONST
})
})

Expand Down
18 changes: 11 additions & 7 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1369,14 +1369,15 @@ export function compileScript(
if (isTS) {
// move all Type declarations to outer scope
if (
(node.type.startsWith('TS') ||
(node.type === 'ExportNamedDeclaration' &&
node.exportKind === 'type') ||
(node.type === 'VariableDeclaration' && node.declare)) &&
node.type !== 'TSEnumDeclaration'
node.type.startsWith('TS') ||
(node.type === 'ExportNamedDeclaration' &&
node.exportKind === 'type') ||
(node.type === 'VariableDeclaration' && node.declare)
) {
recordType(node, declaredTypes)
hoistNode(node)
if (node.type !== 'TSEnumDeclaration') {
hoistNode(node)
}
}
}
}
Expand Down Expand Up @@ -1957,7 +1958,10 @@ interface PropTypeData {
}

function recordType(node: Node, declaredTypes: Record<string, string[]>) {
if (node.type === 'TSInterfaceDeclaration') {
if (
node.type === 'TSInterfaceDeclaration' ||
node.type === 'TSEnumDeclaration'
) {
declaredTypes[node.id.name] = [`Object`]
} else if (node.type === 'TSTypeAliasDeclaration') {
declaredTypes[node.id.name] = inferRuntimeType(
Expand Down

0 comments on commit 2e074a7

Please sign in to comment.