Recently i migrated our vue.js2.x project to typescript, and based on evan-you's comment in this issue:
https://github.com/vuejs/rfcs/pull/17#issuecomment-494242121
The Class API proposal is being dropped. so i'm using regular option based version of vue.js which it faced me with alot of type problems.
I want to know is vuejs2.x composition api fully compatibile with typescript? and should i use it to solve all the problem? what is the best practice to do here in this situation?
I'm unsure what "fully compatible with TypeScript" means. But you can definitely use TypeScript with the Vue composition API, and TypeScript helps to improve the code and the developer experience.
Here is an example using the composition plugin with Vue 2:
import { computed, createComponent, reactive } from "@vue/composition-api";
export default createComponent({
name: "Hello",
template: `<p>{{ state.message }}</p>`,
props: {
name: {
type: String,
required: true
}
},
setup(props, context) {
const state = reactive({
message: computed(() => `Hello, ${props.name}!`)
});
return {
state
};
}
});
All the above code is well typed. The composition API (here: createComponent
, reactive
, computed
) is provided with correct types. Notice that with the composition API, we don't need to use this
anymore.