You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/docs/manual/latest/module-functions.mdx
+8-8
Original file line number
Diff line number
Diff line change
@@ -99,23 +99,23 @@ export {
99
99
100
100
## Sharing a type with an external binding
101
101
This becomes incredibly useful when you need to have types that are unique to a project but shared across multiple components.
102
-
Let's say you want to create a library with a `useEnv` hook to load in environment variables found in `import.meta.env`.
102
+
Let's say you want to create a library with a `getEnv` function to load in environment variables found in `import.meta.env`.
103
103
```res
104
104
@val external env: 'a = "import.meta.env"
105
105
106
-
let useEnv = () => {
106
+
let getEnv = () => {
107
107
env
108
108
}
109
109
```
110
-
It's not possible to define types for this that will work for every project, so we just set it as 'a and the consumer of our library can define the return type when they use the hook.
110
+
It's not possible to define types for this that will work for every project, so we just set it as 'a and the consumer of our library can define the return type when they use the function.
111
111
```res
112
112
type t = {"LOG_LEVEL": string}
113
113
114
-
let values: t = useEnv()
114
+
let values: t = getEnv()
115
115
```
116
116
This isn't great and it doesn't take advantage of ReScript's type system and ability to use types without type definitions, and it can't be easily shared across our application.
117
117
118
-
We can instead create a module function that can return a module that has contains a `useEnv` hook that has a typed response.
118
+
We can instead create a module function that can return a module that has contains a `getEnv` hook that has a typed response.
0 commit comments