typesafe-storage

Typesafe Web Storage API wrapper to support objects and arrays

Usage no npm install needed!

<script type="module">
  import typesafeStorage from 'https://cdn.skypack.dev/typesafe-storage';
</script>

README

typesafe-storage

Typesafe Web Storage API wrapper to support objects and arrays.

License Actions Status NPM Version Downloads Month Downloads Total Dependencies Status Semantic Release Commitizen Friendly PRs Welcome

All Contributors

Installation

npm install typesafe-storage

# or

yarn add typesafe-storage

Usage

CodeSandbox

import { createStorage } from "typesafe-storage";

const storage = createStorage<{
  string: string;
  number: number;
  object: {
    string: string;
    number: number;
  };
  array: (string | number)[];
}>(localStorage);

storage.setItem("string", "value");
storage.setItem("number", 1);
storage.setItem("number", "error"); // ❌: Argument of type '"error"' is not assignable to parameter of type 'number'.
storage.setItem("object", {
  string: "value",
  number: 1
});
storage.setItem("array", ["value", 1]);
storage.setItem("notExistKey", "value"); // ❌: Argument of type '"notExistKey"' is not assignable to parameter of type '"string" | "number" | "object" | "array"'.

storage.getItem("string");
storage.getItem("number");
storage.getItem("object");
storage.getItem("array");
storage.getItem("notExistKey"); // ❌: Argument of type '"notExistKey"' is not assignable to parameter of type '"string" | "number" | "object" | "array"'.

storage.removeItem("string");
storage.removeItem("number");
storage.removeItem("object");
storage.removeItem("array");
storage.removeItem("notExistKey"); // ❌: Argument of type '"notExistKey"' is not assignable to parameter of type '"string" | "number" | "object" | "array"'.

storage.clear();

Contributing

Contributions are always welcome! Please read the contributing first.

Contributors

Thanks goes to these wonderful people (emoji key):


Kotaro Sugawara

💻 📖 🤔 🚇 ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

License

MIT © Kotaro Sugawara