Skip to content

Docs for Type module #32

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

Merged
merged 2 commits into from
Feb 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/Core__Type.resi
Original file line number Diff line number Diff line change
@@ -1,12 +1,62 @@
/***
Utilities for classifying the type of JavaScript values at runtime.
*/

/**
The possible types of JavaScript values.
*/
type t = [#undefined | #object | #boolean | #number | #bigint | #string | #symbol | #function]

/**
`typeof(someValue)`

Returns the underlying JavaScript type of any runtime value.

See [`typeof`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) on MDN.

## Examples
```rescript
Console.log(Type.typeof("Hello")) // Logs "string" to the console.

let someVariable = true

switch someVariable->Type.typeof {
| #boolean => Console.log("This is a bool, yay!")
| _ => Console.log("Oh, not a bool sadly...")
}
```
*/
external typeof: 'a => t = "#typeof"

module Classify: {
/***
Classifies JavaScript runtime values.
*/

/**
An abstract type representing a JavaScript function.

See [`function`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) on MDN.
*/
type function

/**
An abstract type representing a JavaScript object.

See [`object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) on MDN.
*/
type object

/**
An abstract type representing a JavaScript symbol.

See [`symbol`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) on MDN.
*/
type symbol

/**
The type representing a classified JavaScript value.
*/
type t =
| Bool(bool)
| Null
Expand All @@ -17,5 +67,17 @@ module Classify: {
| Function(function)
| Symbol(symbol)

/**
`classify(anyValue)`
Classifies a JavaScript value.

## Examples
```rescript
switch %raw(`null`)->Type.Classify.classify {
| Null => Console.log("Yup, that's null.")
| _ => Console.log("This doesn't actually appear to be null...")
}
```
*/
let classify: 'a => t
}