|
| 1 | +--- |
| 2 | +title: Module Type Structure |
| 3 | +description: Learn more about the module structure of @rescript/webapi. |
| 4 | +slug: "03-module-type-structure" |
| 5 | +--- |
| 6 | + |
| 7 | +import { Aside, FileTree, Code } from "@astrojs/starlight/components"; |
| 8 | + |
| 9 | +Every interface in a Web API module can potentially contain methods. These methods are modeled in a separate module named after the interface. |
| 10 | + |
| 11 | +The primary reason for this separation is to handle method overloads. |
| 12 | +As explained in the [Design Philosophy](../design-philosophy) section, ReScript does not permit records to define the same properties more than once. |
| 13 | +Therefore, methods with overloads cannot be modeled within the same record type. |
| 14 | + |
| 15 | +## Bindings |
| 16 | + |
| 17 | +Another advantage of having a separate file is that these bindings can utilize all types defined in the API module. |
| 18 | +Under normal circumstances, the type module only contains `@send` bindings where the type is the first parameter. |
| 19 | + |
| 20 | +<FileTree> |
| 21 | + |
| 22 | +- DOMAPI |
| 23 | + - HTMLButtonElement.res |
| 24 | + |
| 25 | +</FileTree> |
| 26 | + |
| 27 | +```ReScript |
| 28 | +/** |
| 29 | +Returns whether a form will validate when it is submitted, without having to submit it. |
| 30 | +[Read more on MDN]( |
| 31 | + https://developer.mozilla.org/docs/Web/API/HTMLButtonElement/checkValidity) |
| 32 | +*/ |
| 33 | +@send |
| 34 | +external checkValidity: htmlButtonElement => bool = "checkValidity" |
| 35 | +``` |
| 36 | + |
| 37 | +## Inheritance |
| 38 | + |
| 39 | +When an interface inherits from another interface, the base interface methods can be [included](https://rescript-lang.org/syntax-lookup#include) into the inheriting interface. |
| 40 | +All methods from [HTMLElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement#instance_methods) should also be available on [HTMLButtonElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLButtonElement#instance_methods). |
| 41 | + |
| 42 | +export const htmlElementModule = ` |
| 43 | +open DOMAPI |
| 44 | +
|
| 45 | +// A concrete type for \`T.t\` is passed later using the \`include\` keyword. |
| 46 | +module Impl = (T: { type t }) => { |
| 47 | +
|
| 48 | +/** |
| 49 | +[Read more on MDN](https://developer.mozilla.org/docs/Web/API/HTMLElement/focus) |
| 50 | +*/ |
| 51 | +@send |
| 52 | +external focus: (T.t, ~options: focusOptions=?) => unit = "focus" |
| 53 | +
|
| 54 | +} |
| 55 | +
|
| 56 | +include Impl({ type t = htmlElement }) |
| 57 | +`; |
| 58 | + |
| 59 | +<Code |
| 60 | + code={htmlElementModule} |
| 61 | + title="DOMAPI/HTMLElement.res" |
| 62 | + lang="ReScript" |
| 63 | +></Code> |
| 64 | + |
| 65 | +export const buttonModule = ` |
| 66 | +open DOMAPI |
| 67 | +
|
| 68 | +// Include all the methods from HTMLElement |
| 69 | +include HTMLElement.Impl({ type t = htmlButtonElement }) |
| 70 | +
|
| 71 | +// Add additional methods specific to HTMLButtonElement: |
| 72 | +
|
| 73 | +/** |
| 74 | +Returns whether a form will validate when it is submitted, without having to submit it. |
| 75 | +[Read more on MDN]( |
| 76 | + https://developer.mozilla.org/docs/Web/API/HTMLButtonElement/checkValidity) |
| 77 | +*/ |
| 78 | +@send |
| 79 | +external checkValidity: htmlButtonElement => bool = "checkValidity" |
| 80 | +`; |
| 81 | + |
| 82 | +<Code |
| 83 | + code={buttonModule} |
| 84 | + title="DOMAPI/HTMLButtonElement.res" |
| 85 | + lang="ReScript" |
| 86 | +></Code> |
0 commit comments