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

Enhance exported types #66

Closed
fsmaia opened this issue Jul 24, 2020 · 4 comments
Closed

Enhance exported types #66

fsmaia opened this issue Jul 24, 2020 · 4 comments

Comments

@fsmaia
Copy link
Contributor

fsmaia commented Jul 24, 2020

Public method parameters and return types could be exported too, so when wrapping then in another method it is possible to specify return types.

Example:

type LocalStorageHook<TValue> = [TValue | null, (newValue: TValue) => void, () => void];

export const createPersistenceHook = <TData>(entity: string) => (): LocalStorageHook<TData> => {
  return useLocalStorage<TData>(entity);
};

In this case, I had to copy the return signature. If LocalStorageHook was exported as a type from the library, this wouldn't be necessary. :)

Pro-tip: when using @microsoft/api-extractor this already becomes a code smell, and automatically generate markdown/DocFX docs

Pro-tip 2: In LocalStorageHook we create a type for each tuple member, like [StorageValue, WriteStorage,DeleteFromStorage], it enhances the documentation too o/

I may help with PRs if you will.

@jharrilim
Copy link
Collaborator

Yes we should export the return types as well. I did not realize it at first, but it seems like we cannot extract the proper return type since we cannot specific the generic value for the function.

Right now doing

type LocalStorageReturnValue = ReturnType<typeof useLocalStorage>;

yields

type LocalStorageReturnValue = [unknown, (newValue: unknown) => void, () => void]

TypeScript doesn't let us do:

Type LocalStorageReturnValue = ReturnType<typeof useLocalStorage<string>>; // syntax error

and I don't believe there is anyway to make that generic https://stackoverflow.com/questions/50321419/typescript-returntype-of-generic-function

@jharrilim
Copy link
Collaborator

Added return types in 2b85b87

@fsmaia
Copy link
Contributor Author

fsmaia commented Aug 11, 2020

Amazing! Thanks 🚀

@StephenKennedy5
Copy link

StephenKennedy5 commented Feb 10, 2023

How would I be able to import the return type for

export type LocalStorageNullableReturnValue<TValue> = [TValue | null, (newValue: TValue | null) => void, () => void];
export type LocalStorageReturnValue<TValue> = [TValue, (newValue: TValue | null) => void, () => void];

from it was exported in use-localstoreage.ts but was not exported in index.ts, I am only able to import from index.ts.

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

No branches or pull requests

3 participants