Enhance exported types
fsmaia opened this issue · comments
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.
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
Amazing! Thanks 🚀
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.