Skip to content

Commit a7b5649

Browse files
fix(language-core): drop undefined from optional prop type with default in template (#5339)
Co-authored-by: KazariEX <1364035137@qq.com>
1 parent f786da7 commit a7b5649

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import type * as ts from 'typescript';
21
import type { VueCompilerOptions } from '../types';
32
import { getSlotsPropertyName } from '../utils/shared';
43
import { endOfLine } from './utils';
54

6-
export function getLocalTypesGenerator(compilerOptions: ts.CompilerOptions, vueCompilerOptions: VueCompilerOptions) {
5+
export function getLocalTypesGenerator(vueCompilerOptions: VueCompilerOptions) {
76
const used = new Set<string>();
87

98
const OmitKeepDiscriminatedUnion = defineHelper(
@@ -19,7 +18,7 @@ type __VLS_OmitKeepDiscriminatedUnion<T, K extends keyof any> = T extends any
1918
() => `
2019
type __VLS_WithDefaults<P, D> = {
2120
[K in keyof Pick<P, keyof P>]: K extends keyof D
22-
? ${PrettifyLocal.name}<P[K] & { default: D[K]}>
21+
? ${PrettifyLocal.name}<P[K] & { default: D[K] }>
2322
: P[K]
2423
};
2524
`.trimStart()
@@ -59,19 +58,10 @@ type __VLS_PropsChildren<S> = {
5958
);
6059
const TypePropsToOption = defineHelper(
6160
`__VLS_TypePropsToOption`,
62-
() => compilerOptions.exactOptionalPropertyTypes ?
63-
`
64-
type __VLS_TypePropsToOption<T> = {
65-
[K in keyof T]-?: {} extends Pick<T, K>
66-
? { type: import('${vueCompilerOptions.lib}').PropType<T[K]> }
67-
: { type: import('${vueCompilerOptions.lib}').PropType<T[K]>, required: true }
68-
};
69-
`.trimStart() :
70-
`
71-
type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;
61+
() => `
7262
type __VLS_TypePropsToOption<T> = {
7363
[K in keyof T]-?: {} extends Pick<T, K>
74-
? { type: import('${vueCompilerOptions.lib}').PropType<__VLS_NonUndefinedable<T[K]>> }
64+
? { type: import('${vueCompilerOptions.lib}').PropType<Required<T>[K]> }
7565
: { type: import('${vueCompilerOptions.lib}').PropType<T[K]>, required: true }
7666
};
7767
`.trimStart()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface HelperType {
1212
export type ScriptCodegenContext = ReturnType<typeof createScriptCodegenContext>;
1313

1414
export function createScriptCodegenContext(options: ScriptCodegenOptions) {
15-
const localTypes = getLocalTypesGenerator(options.compilerOptions, options.vueCompilerOptions);
15+
const localTypes = getLocalTypesGenerator(options.vueCompilerOptions);
1616
const inlayHints: InlayHintInfo[] = [];
1717

1818
return {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script setup lang="ts">
2+
import { exactType } from '../shared';
3+
4+
withDefaults(defineProps<{
5+
foo?: string;
6+
bar?: string;
7+
}>(), {
8+
foo: 'foo',
9+
});
10+
</script>
11+
12+
<template>
13+
{{ exactType(foo, {} as string) }}
14+
{{ exactType(bar, {} as string | undefined) }}
15+
</template>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"exactOptionalPropertyTypes": true
5+
},
6+
"vueCompilerOptions": {
7+
"target": 3.5
8+
},
9+
"include": [ "**/*" ]
10+
}

0 commit comments

Comments
 (0)