3 ms·
I was hoping for better Typescript support for typing properties, since that is where 90% of our type errors occur. But it seems like you still have to specify
by IshKebab 6y ago
I was hoping for better Typescript support for typing properties, since that is where 90% of our type errors occur. But it seems like you still have to specify the types manually. The example from the manual:
const Component = defineComponent({
props: {
name: String,
success: { type: String },
callback: {
type: Function as PropType<() => void>
},
message: {
type: Object as PropType<ComplexMessage>,
required: true,
validator(message: ComplexMessage) {
return !!message.title
}
}
}
})
Contrast this with React where this would be something like this (I think; I've never used React):
interface Props {
name: string;
success: string;
callback: () => void;
message: ComplexMessage;
}
export default class Component extends React.Component<Props> {
...
Quite disappointing. Maybe not that surprising given Evan said he only started using Typescript very recently, and many beginner Javascript developers don't realise that they should really be using Typescript (fortunately Evan isn't one of them). I would recommend still avoiding Vue for this reason alone.
- EvanYou 6y agoIt's not that we can't implement it like that, the real challenge is in minimizing breakage from v2. We decided it's better to not completely alter how props are declared because that would be too much breakage. Instead, there's the compiler-based approach with `<script setup lang="ts">`: https://docs.google.com/presentation/d/1VjBM6ae-fuawK1TltYLXtcXGaxJw1y8ncT6wnbjaBGo/edit#slide=id.g86a2c92822_0_168 https://docs.google.com/presentation/d/1VjBM6ae-fuawK1TltYLX... (runtime props definitions auto-generated from TS interface)
- mthoms 6y agoFYI Evan, that doc is currently private.
- solarmist 6y agoThe google doc is restricted.
- IshKebab 6y agoBut if the prop definitions are generated at runtime that means Typescript can't check them at compile time surely? Better than Vue 2 I guess but still not as good as React. Fair enough on not wanting to break things, but I still feel like it is Vue's second biggest flaw and worth breaking to fix.
- dbfa 6y agoMore detailed info: https://github.com/vuejs/rfcs/blob/sfc-improvements/active-rfcs/0000-sfc-script-setup.md https://github.com/vuejs/rfcs/blob/sfc-improvements/active-r...
- Udik 6y agoYou can use vue-class-component and vue-property-decorator as described here: https://class-component.vuejs.org/ https://class-component.vuejs.org/ You get to define your components as classes with their methods and properties, and can declare props and watchers via decorators. Overall a good typescript experience.
- IshKebab 6y agoThat is what I currently do. It's ok within a component, but there's no type checking when another component uses yours and gets the prop types wrong.