Skip to content

Commit

Permalink
fix: event "@update:" syntax causes TS error
Browse files Browse the repository at this point in the history
close #3100
  • Loading branch information
johnsoncodehk committed May 1, 2023
1 parent 927d564 commit 659331c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/vue-language-core/src/generators/template.ts
Expand Up @@ -989,7 +989,7 @@ export function generate(
&& prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
) {
codes.push(
camelize('on-' + prop.arg.loc.source),
...createObjectPropertyCode(camelize('on-' + prop.arg.loc.source)),
': {} as any, ',
);
}
Expand Down Expand Up @@ -1679,12 +1679,12 @@ export function generate(
];
}

function createObjectPropertyCode(a: Code, astHolder: any): Code[] {
function createObjectPropertyCode(a: Code, astHolder?: any): Code[] {
const aStr = typeof a === 'string' ? a : a[0];
if (validTsVar.test(aStr)) {
return [a];
}
else if (aStr.startsWith('[') && aStr.endsWith(']')) {
else if (aStr.startsWith('[') && aStr.endsWith(']') && astHolder) {
const range = typeof a === 'object' ? a[2] : undefined;
const data = typeof a === 'object' ? a[3] : undefined;
return createInterpolationCode(
Expand Down
26 changes: 26 additions & 0 deletions packages/vue-test-workspace/vue-tsc/#3100/comp.vue
@@ -0,0 +1,26 @@
<template>
<div class="modal" :class="{ 'modal-large': large }" v-show="show">
<div>
<slot />
</div>
<div>
<button type="button" @click="$emit('close')">close</button>
<button type="button" @click="$emit('update:show', false)">update:show</button>
</div>
</div>
</template>

<script setup lang="ts">
defineEmits(['close', 'update:show'])
defineProps({
show: {
type: Boolean,
default: false
},
large: {
type: Boolean,
default: false
}
})
</script>
26 changes: 26 additions & 0 deletions packages/vue-test-workspace/vue-tsc/#3100/main.vue
@@ -0,0 +1,26 @@
<script setup lang="ts">
import { ref } from 'vue'
import ModalDialog from './comp.vue'
const isModalOpen1 = ref(false)
const isModalOpen2 = ref(false)
</script>

<template>
<div>
<div>
<button @click="isModalOpen1 = true">Show Dialog 1 (<code>close</code> event)</button>
<ModalDialog :show="isModalOpen1" :large="true" @close="isModalOpen1 = false">
This dialog uses the <code>close</code> event to close
</ModalDialog>
</div>

<div>
<button @click="isModalOpen2 = true">Show Dialog 2 (<code>update:show</code> event)</button>
<!-- errors are on 'show' and 'large' props here -->
<ModalDialog :show="isModalOpen2" :large="true" @update:show="isModalOpen2 = $event">
This dialog uses the <code>update:show</code> event to close
</ModalDialog>
</div>
</div>
</template>

0 comments on commit 659331c

Please sign in to comment.