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 1 commit
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
Prev Previous commit
Next Next commit
fix: correct handling of exactOptionalPropertyTypes in `__VLS_TypeP…
…ropsToOption`
  • Loading branch information
KazariEX committed Apr 27, 2025
commit 039cd71753a96ebf03a9a5d2eda8b031462854fd
16 changes: 3 additions & 13 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 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
13 changes: 13 additions & 0 deletions test-workspace/tsc/passedFixtures/#5338/main.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script setup lang="ts">
import { exactType } from '../shared';

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

<template>
{{ exactType(foo, {} as string) }}
</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": [ "**/*" ]
}