Avoiding having to import('../types')
all over the place
#15
Unanswered
HenrikJoreteg
asked this question in
Questions
Replies: 2 comments 4 replies
-
I did not liked introducing globals so I kept doing lib.js (actual implementation lives here)import * as API from "./result.js"
/**
* @template T
* @param {T} value
* @returns {API.Result<never, T>}
*/
export ok = (value) => ({ ok: true, value })
// ... result.js (just a facade for lib.js)export * from "./lib.js" result.ts (shadows result.js and declares all the types)export * from "./lib.js"
export type Result<X, T> =
| { ok: true, value: T }
| { ok: false, error: X } Now any other module can import module and all the types as follows: import * as Result form "../path/to/result.js"
/**
* @template X, T
* @param {Result.Result<X, T>} result
* @param {(t:T) => U} f
* @returns {Result.Result<X, U>}
*/
export const map = (result, f) =>
result.ok ? Result.ok(f(result.value)) : result It is not perfect and I wish I did not have to have facade |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey all! 👋
Is there a way to declare that you want types that you export from a root
types.d.ts
to just be available from your JS without needing to import them?I've done this at the top of a file to redeclare the types locally, but would be nice to avoid this. Feels like it should exist and I'm just not doing it right:
Beta Was this translation helpful? Give feedback.
All reactions