Skip to content

fix(language-core): drop undefined from type of optional prop with default in template #5339

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

Merged
merged 6 commits into from
Apr 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions packages/language-core/lib/codegen/localTypes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type * as ts from 'typescript';
import type { VueCompilerOptions } from '../types';
import { getSlotsPropertyName } from '../utils/shared';
import { endOfLine } from './utils';

export function getLocalTypesGenerator(compilerOptions: ts.CompilerOptions, vueCompilerOptions: VueCompilerOptions) {
export function getLocalTypesGenerator(vueCompilerOptions: VueCompilerOptions) {
const used = new Set<string>();

const OmitKeepDiscriminatedUnion = defineHelper(
Expand All @@ -19,7 +18,7 @@ type __VLS_OmitKeepDiscriminatedUnion<T, K extends keyof any> = T extends any
() => `
type __VLS_WithDefaults<P, D> = {
[K in keyof Pick<P, keyof P>]: K extends keyof D
? ${PrettifyLocal.name}<P[K] & { default: D[K]}>
? ${PrettifyLocal.name}<P[K] & { default: D[K] }>
: P[K]
};
`.trimStart()
Expand Down Expand Up @@ -59,19 +58,10 @@ type __VLS_PropsChildren<S> = {
);
const TypePropsToOption = defineHelper(
`__VLS_TypePropsToOption`,
() => compilerOptions.exactOptionalPropertyTypes ?
`
type __VLS_TypePropsToOption<T> = {
[K in keyof T]-?: {} extends Pick<T, K>
? { type: import('${vueCompilerOptions.lib}').PropType<T[K]> }
: { type: import('${vueCompilerOptions.lib}').PropType<T[K]>, required: true }
};
`.trimStart() :
`
type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;
() => `
type __VLS_TypePropsToOption<T> = {
[K in keyof T]-?: {} extends Pick<T, K>
? { type: import('${vueCompilerOptions.lib}').PropType<__VLS_NonUndefinedable<T[K]>> }
? { type: import('${vueCompilerOptions.lib}').PropType<Required<T>[K]> }
: { type: import('${vueCompilerOptions.lib}').PropType<T[K]>, required: true }
};
`.trimStart()
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/script/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface HelperType {
export type ScriptCodegenContext = ReturnType<typeof createScriptCodegenContext>;

export function createScriptCodegenContext(options: ScriptCodegenOptions) {
const localTypes = getLocalTypesGenerator(options.compilerOptions, options.vueCompilerOptions);
const localTypes = getLocalTypesGenerator(options.vueCompilerOptions);
const inlayHints: InlayHintInfo[] = [];

return {
Expand Down
15 changes: 15 additions & 0 deletions test-workspace/tsc/passedFixtures/#5338/main.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script setup lang="ts">
import { exactType } from '../shared';

withDefaults(defineProps<{
foo?: string;
bar?: string;
}>(), {
foo: 'foo',
});
</script>

<template>
{{ exactType(foo, {} as string) }}
{{ exactType(bar, {} as string | undefined) }}
</template>
10 changes: 10 additions & 0 deletions test-workspace/tsc/passedFixtures/#5338/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"exactOptionalPropertyTypes": true
},
"vueCompilerOptions": {
"target": 3.5
},
"include": [ "**/*" ]
}