Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend a JS class in fable

Tags:

f#

fable-f#

Given a class defined in an external JS library (let's call it Foo) then I'd like to extend in F#

type bar() = 
   inherits Foo
   ...

However I can' only find examples of how to integrate with functions

and

[<Import("Foo", from="my-module")>]
let Foo = JsNative

will of course not let me derive Bar from Foo. So how Do I do that

like image 734
Rune FS Avatar asked Jan 18 '26 19:01

Rune FS


1 Answers

The Import attribute can be used on a type declaration. e.g.

[<Import("Foo", from="my-module")>]
type Foo() =
    class
    end

type Bar() =
    inherit Foo()

You can then also include signatures for the members of the class. It's instructive to look at examples of imports like the Fable React declarations: https://github.com/fable-compiler/fable-react/blob/master/src/Fable.React/Fable.Import.React.fs

like image 56
Dave Avatar answered Jan 21 '26 09:01

Dave