I am just starting out with TypeScript and VueJS. Currently, I am pondering the best approach for setting the type of a JSON key that should start off as null.
<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
data() {
return {
value: null
}
},
});
</script>
When I try to set this.value = 123, TypeScript throws an error since it cannot assign a number to null. This is not surprising. My question is, what is the most elegant solution for handling this scenario? One option is to define an interface like this:
interface INullableString { value: number | null; }
<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
data(): INullableString {
return {
value: null
}
},
});
</script>
Is there a more efficient or cleaner way to achieve this?