Skip to content

Commit

Permalink
feat(language-core): support default prop when using __typeProps (#4602)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyuanzmj authored Jul 20, 2024
1 parent 8a685f0 commit 0e5f58d
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 61 deletions.
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/script/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function* generatePropsOption(
if (options.vueCompilerOptions.target >= 3.5 && ctx.generatedPropsType) {
yield `__typeProps: {} as __VLS_PublicProps,${newLine}`;
}
else {
if (options.vueCompilerOptions.target < 3.5 || !ctx.generatedPropsType || scriptSetupRanges.props.withDefaults) {
const codegens: (() => Generator<Code>)[] = [];

if (ctx.generatedPropsType) {
Expand Down
109 changes: 55 additions & 54 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test-workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"vue-component-type-helpers": "2.0.26",
"vue2": "npm:[email protected]",
"vue3.3": "npm:[email protected]",
"vue3.5": "npm:vue@alpha"
"vue3.5": "npm:vue@3.5.0-alpha.3"
}
}
18 changes: 13 additions & 5 deletions test-workspace/tsc/vue3.5/components/main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ const ScriptSetupExposeExact = defineComponent({
const ScriptSetupTypeOnlyExact = defineComponent({
__typeProps: {} as {
foo: string,
bar?: number
bar?: number;
},
__typeEmits: {} as {
(e: 'change', id: number): void
(e: 'update', value: string): void
(e: 'change', id: number): void;
(e: 'update', value: string): void;
},
setup() {
return {};
Expand All @@ -48,8 +48,16 @@ const ScriptSetupTypeOnlyExact = defineComponent({
// https://vuejs.org/api/sfc-script-setup.html#default-props-values-when-using-type-declaration
const ScriptSetupDefaultPropsExact = defineComponent({
__typeProps: {} as {
msg?: string
labels?: string[]
msg?: string;
labels?: string[];
},
props: {
msg: {
default: 'hello'
},
labels: {
default: () => ['one', 'two']
}
},
setup() {
return {};
Expand Down
18 changes: 18 additions & 0 deletions test-workspace/tsc/vue3/withDefaults/main.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script setup lang="ts">
import { exactType } from "tsc/shared";
interface Props {
actionText?: string;
}
withDefaults(defineProps<Props>(), {
actionText: "foo",
});
</script>

<template>
<div>
{{ exactType(actionText, {} as string) }}
{{ exactType($props.actionText, {} as string | undefined) }}
</div>
</template>

0 comments on commit 0e5f58d

Please sign in to comment.