Skip to content

Conversation

@randompch
Copy link

🔍 Context

Fixes #160

🏗 Work

  • Update packages/cookie-universal/types/index.d.ts

🗒 Notes

  1. It's actually been implemented in the nuxt module in 2020 : b97b792

  2. Also, what about overloading the getAll() function ? It would add type safety without breaking changes. 🤔

export interface ICookie {
  // [...]

  getAll(opts: ICookieGetOpts & { parseJSON: false }): Record<string, string>
  getAll(opts: ICookieGetOpts & { parseJSON: true }): Record<string, any>
  getAll<T = Record<string, any>>(opts?: ICookieGetOpts): T

  // [...]
}

Potential usage :

// Should infer Record<string, string>
const strings = cookies.getAll({ parseJSON: false });

// Should infer Record<string, any>
const parsed = cookies.getAll({ parseJSON: true });

// Should allow generic override
const typed = cookies.getAll<SomeCustomType>();

// Should work without options (backward compatible)
const all = cookies.getAll();

The same could be done to the get() function.

I can move this proposal to a dedicated issue if we need to discuss it.
I also can drop it completely 💪

Cheers

@randompch
Copy link
Author

Ping @microcipcip 🫶

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.

[Types] ICookie get and getAll() could benefit from generics

1 participant