Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generic type support #237

Merged
merged 5 commits into from
Jun 21, 2023
Merged

feat: generic type support #237

merged 5 commits into from
Jun 21, 2023

Conversation

maou-shonen
Copy link
Contributor

It would be nice to have generics support,
This can enhance DX.

some examples

import { createStorage } from "./storage";
import { prefixStorage } from "./utils";

// If no type is specified, the behavior remains the same as before
const baseStorage = createStorage(); // Storage<StorageValue>
baseStorage.getItem("k"); // => Promise<StorageValue>
baseStorage.getItemRaw("k"); // => Promise<any>
baseStorage.setItem("k", 1); // value allow StorageValue
baseStorage.setItemRaw("k", 1); // value allow any

// If type is specified
baseStorage.getItem<number>("k"); // => Promise<number>
baseStorage.getItemRaw<number>("k"); // => Promise<number>
baseStorage.setItem<number>("k", 1); // value allow number
baseStorage.setItemRaw<number>("k", 1); // value allow number

// forward reference
const numberStorage = createStorage<number>(); // Storage<number>
numberStorage.getItem("k"); // => Promise<number>
numberStorage.getItemRaw("k"); // => Promise<number>
numberStorage.setItem("k", 1); // => Promise<number>
numberStorage.setItemRaw("k", 1); // => Promise<number>

// overriding incompatible types is not allowed
numberStorage.getItem<string>("k", "1"); // ts error
numberStorage.setItem<string>("k", "1"); // ts error

// custom type
type Foo = {
  a: number;
};
const customTypeStorage = createStorage<Foo>();
customTypeStorage.getItem("k"); // Promise<Foo>
customTypeStorage.setItem("k", { a: 123 }); // value allow Foo

// prefixStorage
prefixStorage(baseStorage, "assets").getItem("k"); // Promise<StorageValue>
prefixStorage<number>(baseStorage, "numbers").getItem("k"); // Promise<number>

Regarding the inability to override,
perhaps it is overly restrictive? What do you think?

// overriding incompatible types is not allowed
numberStorage.getItem<string>("k", "1"); // ts error

Also, my English is weak,
if there are any inaccuracies in the documentation,
anyone is welcome to modify it.

@nuxt-studio
Copy link

nuxt-studio bot commented May 27, 2023

Live Preview ready!

Name Edit Preview Latest Commit
unstorage Edit on Studio ↗︎ View Live Preview fa03a14

@codecov
Copy link

codecov bot commented May 27, 2023

Codecov Report

Merging #237 (fa03a14) into main (7520633) will increase coverage by 0.09%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #237      +/-   ##
==========================================
+ Coverage   80.76%   80.85%   +0.09%     
==========================================
  Files          24       24              
  Lines        2682     2695      +13     
  Branches      446      445       -1     
==========================================
+ Hits         2166     2179      +13     
  Misses        515      515              
  Partials        1        1              
Impacted Files Coverage Δ
src/storage.ts 88.03% <100.00%> (+0.06%) ⬆️
src/types.ts 100.00% <100.00%> (ø)
src/utils.ts 93.65% <100.00%> (+0.31%) ⬆️

@pi0 pi0 changed the title feat: generic support feat: generic type support Jun 21, 2023
@pi0 pi0 merged commit 8ad15c1 into unjs:main Jun 21, 2023
@maou-shonen maou-shonen deleted the generic-support branch June 22, 2023 12:29
so1ve pushed a commit to so1ve/unstorage that referenced this pull request Jul 8, 2023
* fix(prefixStorage): prefix `getItemRaw` and `setItemRaw` (unjs#232)

* fix(github): fetchFiles should return files (unjs#229)

* chore(deps): update all non-major dependencies (unjs#220)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* chore: update eslint

* test: skip cloudflare-kv-http on node >= 18

* chore(release): v1.6.1

* docs: add social share image

* chore: update deps

* docs: fix typo (unjs#239)

Change `environemnt` to `environment`
Update cloudflare-kv-http.md

* chore: update dependencies

* feat: generic type support (unjs#237)

* refactor: fix issues with typescript strict (unjs#250)

* chore: add type check to ci

* ci: skip flaky azure tests

* chore(release): v1.7.0

* chore(deps): update all non-major dependencies

* docs: fix typo (unjs#252)

* chore(deps): update all non-major dependencies

* test: add test for `github` driver (unjs#259)

* feat: experimental operation batching (unjs#240)

Co-authored-by: Pooya Parsa <pooya@pi0.io>

* feat(cloudflare-kv): support `base` option for keys (unjs#261)

* feat: `cloudflare-r2-binding` driver (unjs#235)

* fix: add missing `cloudflareR2Binding` to the `builtinDrivers`

* chore: update dev dependencies

* chore(release): v1.8.0

* Fix typescript checks

* add typehint

* Install execa

* Write test code

* Remove not using import

---------

Co-authored-by: 魔王少年 <q267009886.work@gmail.com>
Co-authored-by: Andrei Dyldin <and@cesbo.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Pooya Parsa <pooya@pi0.io>
Co-authored-by: Sébastien Chopin <seb@nuxt.com>
Co-authored-by: Neelansh Mathur <53081208+neelansh15@users.noreply.github.com>
Co-authored-by: 魔王少年 <q267009886.tw@gmail.com>
Co-authored-by: Alex Duval <alexduval71@gmail.com>
Co-authored-by: Hebilicious <xsh4k3@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants