-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
ref returned by toRefs(props) have type Ref<X | undefined> | undefined #6420
Comments
For However, I think the core issue is TS rather recent differentiation between an optional property and a property whose value is const props = defineProps<{
requiredProperty: string;
optionalProperty: string | undefined;
}>(); ...and it's better reflecting reality as well: the prop can't be missing on that props object, it can only be And from this perspective, the prop should really be optional, not just its valzue possibly undefined - we want to be able to completely omit it in the parent - but the property key should exist internally on the Tricky 🤔 |
This is only valid for I think we can update the return type from |
I agree. But, I think, the main topic of this issue is that it is impossible to separate |
If you have an optional prop (= it can be if you have a piece of code that expects const myProp: Ref<string | undefined> = toRef(props, 'myProp')
function myFn (myProp: Ref<string>) { ... }
if (myProp.value) {
myFn(myProp)
} you would need to do the exact same thing for a possibly undefined plain variable ( |
True, forgot to mention it, but was aware
Great, agreed. |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
same problem。
This type will not be infer to undefined |
because you set |
but . if I code like this。the type will be undefined
|
const aaa = {
form: {
type: Object,
required: true,
},
formOptions: {
type: Array as PropType<FormOptionItem[]>,
required: true,
},
} as const without |
Actual in vue 3.3.4 |
As a workaround for now, I'm casting the return type of const { prop1, prop2 } = toRefs(props) as Required<ReturnType<typeof toRefs<PropInterface>>>; |
Vue version
latest, 3.2.37
Link to minimal reproduction
https://codesandbox.io/s/vigorous-sound-6m9x2d
Steps to reproduce
Check sandbox link provided in minimal reproduction field and run
npm run type-check
in terminal:What is expected?
Expected that the
type-check
action returns no error inbuggedComponent.vue
that isthe type of
optionalProperty
inanyProperty
assignation detects asstring
because all conditions that may detect and prevent undefined type are passed:What is actually happening?
typescript compiler detects type of the
optionalProperty
inanyProperty
assign condition asstring|undefined
System Info
No response
Any additional comments?
If you dont want to check the reproduction link, there are some photos that describes the problem:
The text was updated successfully, but these errors were encountered: