Vue-cli3 is used and typescript, is enabled. Please tell me how to declare the type to the value in data.

<script lang="ts">
import Vue from "vue"
import HelloWorld from "@/components/HelloWorld.vue"

export default Vue.extend({
    components: { HelloWorld },
    data () {
        return {
            bb: "123"
        }
    },
    watch: {
        bb (val: String) {
            console.log(val)
        }
    },
    props: {
        propA: Number
    },
    methods: {
        haha (a: String) {
            console.log(a)
        }
    },
    created () {
    }
})
</script>

at present, everything except data works normally, but there is no clue as to how the content in data declares the type. The official document seems to only give the writing method of "class-based Vue component". However, in order to maintain the consistency with the old project, we do not want to use this method at present.

clipboard.png


set the interface of the return value of the data function

data(): iData{
    return(
        a: ''
    
}
interface iData{
    a: string
}

check out vue-class-component


you can write
`

data(){
    const dataSource: ListItem[] = []
    return {
        dataSource
    }
}

`


bb: string = 'abc'

.
Menu