Skip to content

Reading the output

A generated file is a module rec holding one type per TypeScript declaration. This page shows the shapes you will meet. The mapping reference lists every construct.

Functions and constructors

The values a package exports are static members of its Exports type. A function is imported by name, a class gets a constructor member marked EmitConstructor.

[<AbstractClass>]
[<Erase>]
type Exports =
    [<Import("format", "date-fns")>]
    static member format (date: Date, formatStr: string) : string = nativeOnly

    [<Import("Map", "leaflet"); EmitConstructor>]
    static member Map (element: string, ?options: MapOptions) : Map = nativeOnly

Open the type to call them without the prefix:

open type Glutinum.DateFns.Exports

format (today, "yyyy-MM-dd")

Alias the type to call them under a name of your choice:

type DateFns = Glutinum.DateFns.Exports

DateFns.format (today, "yyyy-MM-dd")

Interfaces and classes

Both become an F# interface with abstract members. A property is with get, set, a method is a member with named parameters.

[<AllowNullLiteral>]
[<Interface>]
type Map =
    abstract member setView: center: LatLng * zoom: float -> Map
    abstract member zoom: float with get, set

The static members of a class are static member inline on the interface, emitted as JavaScript.

Optional members and parameters

An optional property is an option. An optional parameter takes ?:

abstract member timeout: float option with get, set
abstract member connect: host: string * ?port: float -> unit

Unions

A union of string literals is a string enum you match on:

[<RequireQualifiedAccess>]
[<StringEnum(CaseRules.None)>]
type Position =
    | top
    | bottom

Other unions are U2, U3 and so on, from Fable.Core. Pass a value with U2.Case1, read one with a match:

element.doc <- Some(U2.Case1 "text")

match request.params.["id"] with
| U2.Case1 single -> single
| U2.Case2 several -> String.concat "," several

A match works when the cases have distinct runtime types, a string and an array here. A case that is an interface cannot be tested: Fable warns and the branch is never taken.

A union in parameter position is generated as one overload per case, so setView (paris, 13) and setView ((48.85, 2.35), 13) both compile. There is no overload taking the union itself. Past --max-overloads combinations the parameter keeps its union type, and Fable.Core 5.4.0 converts a value of a case to it.

A named union, type PathLike = string | Buffer | URL, is an erased union of its own with a case per type, PathLike.String, PathLike.Buffer, PathLike.URL, an implicit conversion from each case, and an unchecked accessor per case, path.AsString. A case is named Case1 when its type is anonymous or when two cases would share a name. A union written inline stays U2..U9, with the accessors AsCase1..AsCase9 from Glutinum.Types.

Objects you construct

A type literal used as a parameter, options: { radius?: number }, becomes a class with a ParamObject constructor. Named arguments build the object:

L.circleMarker (paris, CircleMarkerOptions(radius = 12))

An interface is built with jsOptions:

jsOptions<MapOptions> (fun options -> options.zoom <- Some 13)

Callbacks

A function type with one parameter is an F# lambda. With several it is a delegate, and F# converts a lambda to it at the call site. A named function type, type Handler = (event: Event) => void, follows the same rule:

app.get ("/users", RequestHandler(fun request response -> response.json users))

Arrays and tuples

An array is a ResizeArray. A readonly array in a return type is ReadonlyArray from Glutinum.Types, and a ResizeArray when it is a parameter, a case of a union with other cases, or the return type of a callback. A tuple is an F# tuple.

Nested types

A type declared inside another, such as the type of an option or an event map, lives in a module named after the parent, Map.Options, Editor.on.

Namespaces

A namespace is a module. Its functions are on the Exports type of the module, its types beside it.

Node.fs.Exports.readFileSync ("file.txt", "utf8")

Enums

A numeric enum is an F# enum. A string enum is a string enum. An enum mixing both is an erased union, its members are constants:

[<RequireQualifiedAccess>]
[<Erase>]
type Level =
    | String of string
    | Number of float
    static member inline Verbose: Level = Level.String "verbose"
    static member inline Error: Level = Level.Number 2.0
Edit this page