Skip to content
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

Inheriting from Error/Exception not possible #351

Open
Booksbaum opened this issue Oct 24, 2020 · 3 comments
Open

Inheriting from Error/Exception not possible #351

Booksbaum opened this issue Oct 24, 2020 · 3 comments

Comments

@Booksbaum
Copy link
Contributor

Inheriting from Error/Exception isn't possible:

export class MyError extends Error { }

gets converted into (abbreviated)

type Error = System.Exception

type [<AllowNullLiteral>] MyError =
    inherit Error

Which results in the error

The type 'Error' is not an interface type

MyError is an interface, while Error/Exception isn't.

Possible fix: make it abstract:

type [<AllowNullLiteral; AbstractClass>] MyError =
    inherit Error

Same for other abbreviated types that aren't interfaces.
Making classes with such inheritance abstract would be an issue when no unit-ctor, or sealed type (Function -> System.Action). And abstract would need to bubble up with more inheritance like class MyOtherError extends MyError {}.

@alarbada
Copy link

Having the same problem, with axios types in my case.

Any suggestion on how to fix it? I'm currently having trouble pattern matching axios errors :(

@MangelMaxime
Copy link
Member

Hello @not-rusty,

if adding AbstractClass works for you, you can just fix the bindings manually.

At least, that's how I did it in my case.

@MangelMaxime
Copy link
Member

Actually, it seems like when adding AbstractClass to a type is different.

open System
open Fable.Core

type [<AllowNullLiteral; >] DatabaseError =
    // inherit System.Exception
    abstract code: string option

let x = unbox<DatabaseError> null

let code = x.code

generates

export const x = null;

export const code = x.code;

but

open System
open Fable.Core

type [<AllowNullLiteral; AbstractClass >] DatabaseError =
    // inherit System.Exception
    abstract code: string option


let x = unbox<DatabaseError> null

let code = x.code

generates

import { class_type } from "fable-library/Reflection.js";

export class DatabaseError {
    constructor() {
    }
}

export function DatabaseError$reflection() {
    return class_type("Test.DatabaseError", void 0, DatabaseError);
}

export const x = null;

export const code = x["Test.DatabaseError.get_code"]();

which is not what we want in the case of a bindings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants