-
-
Notifications
You must be signed in to change notification settings - Fork 438
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
Conversation
vue-component-meta
vue-component-type-helpers
@vue/language-core
@vue/language-plugin-pug
@vue/language-server
@vue/language-service
@vue/typescript-plugin
vue-tsc
commit: |
I think the current handling of language-tools/packages/language-core/lib/codegen/localTypes.ts Lines 62 to 77 in 48c4578
It should be: type __VLS_TypePropsToOption<T> = {
[K in keyof T]-?: {} extends Pick<T, K>
? { type: import('${vueCompilerOptions.lib}').PropType<Required<T>[K]> }
: { type: import('${vueCompilerOptions.lib}').PropType<T[K]>, required: true }
}; So that we don't need to make any changes to |
Unfortunately, I don't think that will work, because it would mark optional props that don't have a default as not possibly const props = withDefaults(
defineProps<{
stringWithDefault?: string;
stringWithDefaultCanBeUndefined?: string | undefined
optionalStringWithoutDefault?: string
}>(),
{
stringWithDefault: 'defaultValue',
stringWithDefaultCanBeUndefined: "defaultVal"
}
);
That's why I made my changes to Thank you for helping look into this! |
It should be optional by default when there is no |
This reverts commit 8208cd9.
I added test case so that you can intuitively see the correct behavior after modifications. Thanks for your contribution! |
__VLS_ctx
undefined
from type of optional prop with default in template
Currently, Optional prop with default may incorrectly be undefined in template when
exactOptionalPropertyTypes
andstrictNullChecks
are true: #5338Here is a Playground link to explore the change: Vue SFC Playground
(The same thing can be seen when compiling locally, and I can provide a full repository if it would help. I don't know how to apply the patch to the VSCode extension/IDE, so I've only been able to test it when running
vue-tsc
)While looking into the issue I reported above, I noticed that the
__VLS_WithDefaults
type inlanguage-core/lib/codegen/localTypes.ts
would still keep a prop as possibly undefined even if a default value was provided. This would be visible when trying to access that prop in the template, where a typescript error would be thrown because it believes the prop could possibly be undefined. The error would not show up if accessing the prop through the props object, only when referencing it directly. See the issue for more informationThis is my first time diving into internal Vue code, so please let me know if there's anything I should do differently!