As a newcomer to TypeScript on my first day, I have experience working on JavaScript projects above the average level. I am seeking advice on best practices and standards for declaring or assigning types in code. Currently, I tend to specify types for everything, which ends up making my code hard to read.
For instance, take a look at this:
import {createApp, DefineComponent} from 'vue'
import {createRouter, createWebHistory, Router, RouteRecordRaw, RouterHistory} from 'vue-router'
import NPage from '~/layouts/N-Page.vue'
const router : Router = createRouter({
history: <RouterHistory>createWebHistory(),
routes: <RouteRecordRaw[]>[{
component: <DefineComponent>NPage,
name: <string>'home',
path: <string>'/',
}]
})
createApp(NPage).use(router).mount('#app')
This represents a basic main.ts
setup for Vue.js. Despite my IDE (IntelliJ IDEA) automatically inferring the type for router
, I still explicitly declared it. The same goes for history
, routes
, and so on. To make matters worse, I often find myself specifying types for each individual property (component
, name
, path
).
This approach is leading to a messy codebase quickly. It seems apparent that this may not be the correct way to go about it. In light of this, could someone guide me on how to determine when we should declare/assign types and when we shouldn't?