Skip to content

Commit 892050c

Browse files
committed
fix: withDefaults and bypass boolean casting
1 parent 310d10f commit 892050c

File tree

7 files changed

+34
-18
lines changed

7 files changed

+34
-18
lines changed

packages/language-core/lib/codegen/globalTypes.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ export function generateGlobalTypes(options: VueCompilerOptions) {
136136
[K in Exclude<keyof T, keyof __VLS_GlobalDirectives> & string as \`v\${Capitalize<K>}\`]: T[K];
137137
};
138138
type __VLS_PrettifyGlobal<T> = { [K in keyof T as K]: T[K]; } & {};
139+
type __VLS_WithDefaultsGlobal<P, D> = {
140+
[K in keyof P as K extends keyof D ? K : never]-?: P[K];
141+
} & {
142+
[K in keyof P as K extends keyof D ? never : K]: P[K];
143+
};
139144
type __VLS_UseTemplateRef<T> = Readonly<import('${lib}').ShallowRef<T | null>>;
140145
type __VLS_ProxyRefs<T> = import('${lib}').ShallowUnwrapRef<T>;
141146

packages/language-core/lib/codegen/localTypes.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { endOfLine } from './utils';
55
export function getLocalTypesGenerator(vueCompilerOptions: VueCompilerOptions) {
66
const used = new Set<string>();
77

8-
const WithDefaults = defineHelper(
9-
`__VLS_WithDefaults`,
8+
const WithDefaultsLocal = defineHelper(
9+
`__VLS_WithDefaultsLocal`,
1010
() =>
1111
`
12-
type __VLS_WithDefaults<P, D> = {
12+
type __VLS_WithDefaultsLocal<P, D> = {
1313
[K in keyof Pick<P, keyof P>]: K extends keyof D
1414
? ${PrettifyLocal.name}<P[K] & { default: D[K] }>
1515
: P[K]
@@ -69,7 +69,7 @@ type __VLS_TypePropsToOption<T> = {
6969
);
7070
const helpers = {
7171
[PrettifyLocal.name]: PrettifyLocal,
72-
[WithDefaults.name]: WithDefaults,
72+
[WithDefaultsLocal.name]: WithDefaultsLocal,
7373
[WithSlots.name]: WithSlots,
7474
[PropsChildren.name]: PropsChildren,
7575
[TypePropsToOption.name]: TypePropsToOption,
@@ -85,8 +85,8 @@ type __VLS_TypePropsToOption<T> = {
8585
get PrettifyLocal() {
8686
return PrettifyLocal.name;
8787
},
88-
get WithDefaults() {
89-
return WithDefaults.name;
88+
get WithDefaultsLocal() {
89+
return WithDefaultsLocal.name;
9090
},
9191
get WithSlots() {
9292
return WithSlots.name;

packages/language-core/lib/codegen/script/component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export function* generatePropsOption(
142142
const propsType = `${ctx.localTypes.TypePropsToOption}<__VLS_PublicProps>`;
143143
return `{} as ` + (
144144
scriptSetupRanges.withDefaults?.arg
145-
? `${ctx.localTypes.WithDefaults}<${propsType}, typeof __VLS_withDefaultsArg>`
145+
? `${ctx.localTypes.WithDefaultsLocal}<${propsType}, typeof __VLS_defaults>`
146146
: propsType
147147
);
148148
});
@@ -163,7 +163,7 @@ export function* generatePropsOption(
163163
options.vueCompilerOptions.target >= 3.6
164164
&& scriptSetupRanges.withDefaults?.arg
165165
) {
166-
yield `__defaults: __VLS_withDefaultsArg,${newLine}`;
166+
yield `__defaults: __VLS_defaults,${newLine}`;
167167
}
168168
yield `__typeProps: `;
169169
yield* generateSpreadMerge(typeOptionCodes);

packages/language-core/lib/codegen/script/scriptSetup.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ function* generateSetupFunction(
301301

302302
if (scriptSetupRanges.defineProps?.typeArg && scriptSetupRanges.withDefaults?.arg) {
303303
// fix https://github.com/vuejs/language-tools/issues/1187
304-
yield `const __VLS_withDefaultsArg = (function <T>(t: T) { return t })(`;
304+
yield `const __VLS_defaults = (function <T>(t: T) { return t })(`;
305305
yield generateSfcBlockSection(
306306
scriptSetup,
307307
scriptSetupRanges.withDefaults.arg.start,
@@ -437,7 +437,7 @@ function* generateComponentProps(
437437
scriptSetupRanges: ScriptSetupRanges,
438438
): Generator<Code> {
439439
if (scriptSetupRanges.defineModel.length) {
440-
yield `const __VLS_defaults = {${newLine}`;
440+
yield `const __VLS_defaultModels = {${newLine}`;
441441
for (const defineModel of scriptSetupRanges.defineModel) {
442442
if (!defineModel.defaultValue) {
443443
continue;
@@ -546,7 +546,7 @@ function* generateDefineModelType(
546546
}
547547
else if (defineModel.defaultValue && propName) {
548548
// Infer from defineModel({default: T})
549-
yield `typeof __VLS_defaults['${propName}']`;
549+
yield `typeof __VLS_defaultModels['${propName}']`;
550550
}
551551
else {
552552
yield `any`;

packages/language-core/lib/codegen/script/template.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,24 @@ function* generateTemplateCtx(
6767
exps.push(`{} as { $emit: ${emitTypes.join(' & ')} }`);
6868
}
6969

70+
const { defineProps, withDefaults } = options.scriptSetupRanges ?? {};
71+
const props = defineProps?.arg
72+
? `typeof ${defineProps.name ?? `__VLS_props`}`
73+
: defineProps?.typeArg && withDefaults?.arg
74+
? `__VLS_WithDefaultsGlobal<__VLS_Props, typeof __VLS_defaults>`
75+
: undefined;
76+
7077
const propTypes: string[] = [];
71-
if (options.scriptSetupRanges?.defineProps) {
72-
const { defineProps } = options.scriptSetupRanges;
73-
propTypes.push(`__VLS_SpreadMerge<__VLS_PublicProps, typeof ${defineProps.name ?? `__VLS_props`}>`);
78+
if (ctx.generatedPropsType) {
79+
if (props) {
80+
propTypes.push(`__VLS_SpreadMerge<__VLS_PublicProps, ${props}>`);
81+
}
82+
else {
83+
propTypes.push(`__VLS_PublicProps`);
84+
}
7485
}
75-
else if (ctx.generatedPropsType) {
76-
propTypes.push(`__VLS_PublicProps`);
86+
else if (props) {
87+
propTypes.push(props);
7788
}
7889
if (emitTypes.length) {
7990
propTypes.push(`__VLS_EmitProps`);

test-workspace/tsc/passedFixtures/vue3/#3779/main.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ defineProps<{
66
</script>
77

88
<template>
9-
<h1>{{ exactType(optionalBoolean, {} as boolean) }}</h1>
9+
<h1>{{ exactType(optionalBoolean, {} as boolean | undefined) }}</h1>
1010
</template>

test-workspace/tsc/passedFixtures/vue3/#3779/named.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ defineProps<{
66
</script>
77

88
<template>
9-
<h1>{{ exactType(optionalBoolean, {} as boolean) }}</h1>
9+
<h1>{{ exactType(optionalBoolean, {} as boolean | undefined) }}</h1>
1010
</template>

0 commit comments

Comments
 (0)