Replies: 7 comments 5 replies
-
hi @maxbogue, thanks for your report! And this is expected behavior because in compiled code, Related: vuejs/vetur#2504 |
Beta Was this translation helpful? Give feedback.
-
It is certainly not expected behavior from anyone trying to use |
Beta Was this translation helpful? Give feedback.
-
@maxbogue this is just to point out the reason, Vetur may not confirm the correct behavior yet, but in Volar this is design, because this behaves is consistent to typescript: let foo: string | undefined;
if (foo) {
($event: any) => onSelect(foo) // Argument of type 'string | undefined' is not assignable to parameter of type 'string'.ts(2345)
}
function onSelect(foo: string) { } |
Beta Was this translation helpful? Give feedback.
-
I understand that the behavior is consistent with the TypeScript as you have written it there (and the template compiler produces). My point is that from a developer perspective writing a Vue template, it's very unexpected and frustrating to receive this error. The reason it's not the same as the code you wrote there in practice is that |
Beta Was this translation helpful? Give feedback.
-
(Move the vue-tsc issue to volar Discussions, sorry if it causes confusion.) Do you think we should throw error on this code? <button v-if="foo" @click="($event) => onSelect(foo)" /> Actually vue event expression is syntactic sugar to the above code, so we should keep this two line has consistent behavior. <button v-if="foo" @click="onSelect(foo)" />
<button v-if="foo" @click="($event) => onSelect(foo)" /> I believe a catch is that the event expression does not look like a callback function.
I totally agree. However, the correctness of type checking behavior is more important than user experience, especially for infrastructure facilities such as vue-tsc. The correctness here refers to exactly the same behavior as TypeSciprt.
This is essentially a TypeScript problem. In principle, TypeScript can also narrow the type of this code, because if foo is undefined, the arrow function will not be defined. But they didn't do this, I hope TypeScript can be implemented in the future, and then I will update vue-tsc immediately. |
Beta Was this translation helpful? Give feedback.
-
Track: vuejs/core#1359 If vue support TS in template, we will have this choose: |
Beta Was this translation helpful? Give feedback.
-
Just want to say this was a fascinating conversation and I learned a lot from it! I had never considered even the most basic case that is always invalid: <div v-if="returnsNullOrAnObject(data)">
<img :src="returnsNullOrAnObject(data).anyProperty">
</div> This will never pass typechecking as each call to the function get returnsNullOrAnObjectComputed (): SomeObject | null {
return this.returnsNullOrAnObject(this.data);
}
returnsNullOrAnObject (collector: Collector): SomeObject | null {
...
} Now in your template you can safely do this instead to achieve what you originally wanted:
In general my new motto is
|
Beta Was this translation helpful? Give feedback.
-
Given a button like
where
foo
can beFoo | null
and withonSelect
expecting aFoo
I get:Not sure the state of this repo, but given how amazing this tool is otherwise I figured I'd report it!
Beta Was this translation helpful? Give feedback.
All reactions