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

fix(compiler-sfc): add type for props's properties in prod mode #4790

Merged
merged 2 commits into from Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 }
ygj6 marked this conversation as resolved.
Show resolved Hide resolved
},
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