Skip to content

Commit

Permalink
fix(compiler-sfc): add type for props's properties in prod mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed Oct 13, 2021
1 parent 7bb9dd0 commit b77b219
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
Expand Up @@ -80,9 +80,10 @@ exports[`sfc props transform default values w/ type declaration, prod mode 1`] =
export default /*#__PURE__*/_defineComponent({
props: {
foo: { default: 1 },
bar: { default: () => {} },
baz: null
foo: { type: Number, default: 1 },
bar: { type: Object, default: () => {} },
baz: { type: null },
bool: { type: Boolean }
},
setup(__props: any) {
Expand Down
Expand Up @@ -83,17 +83,18 @@ describe('sfc props transform', () => {
const { content } = compile(
`
<script setup lang="ts">
const { foo = 1, bar = {} } = defineProps<{ foo?: number, bar?: object, baz?: any }>()
const { foo = 1, bar = {} } = defineProps<{ foo?: number, bar?: object, baz?: any, bool?: boolean }>()
</script>
`,
{ isProd: true }
)
// literals can be used as-is, non-literals are always returned from a
// function
expect(content).toMatch(`props: {
foo: { default: 1 },
bar: { default: () => {} },
baz: null
foo: { type: Number, default: 1 },
bar: { type: Object, default: () => {} },
baz: { type: null },
bool: { type: Boolean }
}`)
assertCode(content)
})
Expand Down
23 changes: 13 additions & 10 deletions packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -693,8 +693,13 @@ export function compileScript(
defaultString ? `, ${defaultString}` : ``
} }`
} else {
const { type } = props[key]
// production: checks are useless
return `${key}: ${defaultString ? `{ ${defaultString} }` : 'null'}`
return `${key}: { type: ${toRuntimeTypeString(
type
)}${
defaultString ? `, ${defaultString}` : ``
} }`
}
})
.join(',\n ')}\n }`
Expand Down Expand Up @@ -1621,15 +1626,13 @@ function extractRuntimeProps(
m.key.type === 'Identifier'
) {
let type
if (!isProd) {
if (m.type === 'TSMethodSignature') {
type = ['Function']
} else if (m.typeAnnotation) {
type = inferRuntimeType(
m.typeAnnotation.typeAnnotation,
declaredTypes
)
}
if (m.type === 'TSMethodSignature') {
type = ['Function']
} else if (m.typeAnnotation) {
type = inferRuntimeType(
m.typeAnnotation.typeAnnotation,
declaredTypes
)
}
props[m.key.name] = {
key: m.key.name,
Expand Down

0 comments on commit b77b219

Please sign in to comment.