DEV Community

Discussion on: TypeScript is wasting my time

Collapse
 
brense profile image
Rense Bakker

Nothing wrong with learning.

Would not recommend to use the localStorage object like that in Typescript or plain Javascript. How can you be sure you want all the items stored on localstorage or that they will be JSON parsable? It's better to keep a list of localstorage keys for your app somewhere and loop through that, instead of all the keys on the localstorage object.

const cacheKeys = ['cache_item_one', 'cache_item_two', 'etc...']

cacheKeys.forEach(key => {
    const value = localStorage.getItem(key)
    if (value) {
        try {
            store.commit('cache/init', { key, value: JSON.parse(value) })
        } catch(e) {
            // wrap JSON.parse in a try catch block to avoid unexpected crashes
        }
    }
})
Enter fullscreen mode Exit fullscreen mode

For your string literals, try this:

const options = {
    notation: 'compact' as 'compact',
    maximumFractionDigits: 1,
};
Enter fullscreen mode Exit fullscreen mode

Your code duplication issue I'm not too familiar with Nuxt. Do they have Typescript documentation? If so, they'll usually explain how you're supposed to extend the built in interfaces.

No idea what you're trying to do with that URLBuilder =P

I don't think there's a hype around Typescript, but it is slowly growing because people appreciate type safety (or they learn to) and not having as many runtime errors.