Hi,
Why the localStorage from cc.sys have any type? i can’t see any method or information.
Refence:http://docs.cocos2d-x.org/creator/manual/en/advanced-topics/data-storage.html?h=localstorage
any
is a convention in TypeScript because JavaScript is a loosely typed language while TS tries to keep types specific and strict while on top of JS.
https://www.typescriptlang.org/docs/handbook/basic-types.html#any
localStorage
is technically a Storage
class object which truly is just an object but with a few extra methods designed around storing and retrieving items. It should really be labled Object
but the idea behind putting any
is probably because that object might have 100 keys or 1, it might be all strings or all numbers, perhaps a json string, etc. I’m sure they put any
to remind developers that your localStorage does not have to adhere to a specific object structure.
Thank you for your answer @skara
If is technically an storage why don’t have storage type?
Then you can use the methods, let any type in a variable it’s a really bad practice. Doing that you are losing all typescript idea. Localstorage must be a storage type or any type that implements that. If has setItem, getItem, and all methods that must have…