core: Problem typing optional props with exactOptionalPropertyTypes enabled in tsconfig
Vue version
3.2.37
Link to minimal reproduction
Steps to reproduce
See reproduction link for full example.
I declare a property which allows undefined:
const props = defineProps<{
modelValue: number | undefined
}>();
and actually pass undefined:
<template>
<TheComponent v-model="value" />
</template>
<script setup lang="ts">
const value = ref<number | undefined>(undefined);
</script>
What is expected?
Vue doesn’t emit runtime errors for explicitly allowed and used undefined properties.
What is actually happening?
I get the following Vue runtime error in dev mode
[Vue warn]: Invalid prop: type check failed for prop "modelValue". Expected Number | Null, got Undefined
This is because the SFC compiler emits the following property declaration. Note how null was used instead of undefined:
props: {
modelValue: { type: [Number, null], required: true }
},
If I modify the generated code and runtime like below (see <<< and >>>), to allow undefined, it seems to work correctly. But I don’t understand if there are other reasons for not allowing undefined to be a required property value:
// Component
props: {
modelValue: { type: [Number, undefined], required: true }
},
// Vue Runtime
function getType(ctor) {
const match = ctor && ctor.toString().match(/^\s*function (\w+)/);
// <<<<<<<< added `undefined` support >>>>>>>>>>
return match ? match[1] : ctor === null ? "null" : ctor === undefined ? "undefined" : "";
}
function assertType(value, type) {
let valid;
const expectedType = getType(type);
if (isSimpleType(expectedType)) {
const t = typeof value;
valid = t === expectedType.toLowerCase();
if (!valid && t === "object") {
valid = value instanceof type;
}
} else if (expectedType === "Object") {
valid = isObject(value);
} else if (expectedType === "Array") {
valid = isArray$1(value);
} else if (expectedType === "null") {
valid = value === null;
// <<<<<<<< added `undefined` support >>>>>>>>>>
} else if (expectedType === "undefined") {
valid = value === undefined;
} else {
valid = value instanceof type;
}
return {
valid,
expectedType
};
}
System Info
System:
OS: Windows
Binaries:
Node: 18.7.0
npm: 8.15.0
npmPackages:
vue: ^3.2.37 => 3.2.37
Any additional comments?
I’m using undefined when a value is not set by the user, for example empty number <input> or unselected <select>, as I understood to be current recommended practices:
Use undefined. Do not use null. https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines#null-and-undefined
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 5
- Comments: 15 (7 by maintainers)
Links to this issue
Commits related to this issue
- feat: respect `exactOptionalPropertyTypes` with props fix https://github.com/vuejs/core/issues/6532 — committed to vuejs/language-tools by johnsoncodehk 2 years ago
Any progress on this issue?
For Volar/vue-tsc, please track https://github.com/johnsoncodehk/volar/issues/1798.
@pikax So there’s two layers to this:
optional?:prop generates a proper runtime prop definition, while a prop withx | undefineddoesn’t. The PR that was already submitted seems to fix this.exactOptionalPropertyTypesis being used. This has to be solved in Volar.Would you agree?
Okay, so I can kinda reproduce this, but a full repro from your side in a github repo would be apprechiated to make sure we are looking at the same things.
I have:
exactOptionalPropertyTypes: truein my tsconfigvue-tsc(0.40.1)This is HelloWorld.vue …:
This is what I have in the parent’s state:
Both, when used for
msg, will throw the error.I think it is because the type generated for the props object that will be passed to the child is:
while the type from the child component is:
So we could get rid of the question mark, but then we can’t leave the prop completely absent in the parent.
Not sure if this needs to be solved in Vue core, our JSX types or vue-tsc?
/cc @pikax @johnsoncodehk What do you think?