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

[TypeScript] Option<> to Optional Parameter misses unwrap. #3847

Closed
Freymaurer opened this issue Jun 14, 2024 · 5 comments
Closed

[TypeScript] Option<> to Optional Parameter misses unwrap. #3847

Freymaurer opened this issue Jun 14, 2024 · 5 comments

Comments

@Freymaurer
Copy link

Description

I am currently working on typescript transpilation for our library ARCtrl. I want to do this to offer type support for the js native release.

Sadly i get around 850 errors, which i am now trying to resolve one by one. I started investigating in:

dist/ts/Spreadsheet/Metadata/Publication.ts:32:65 

error TS2345: Argument of type 'Option<string>' is not assignable to parameter of type 'string | undefined'.

32     const status_1: OntologyAnnotation = new OntologyAnnotation(status, statusTermSourceREF, statusTermAccessionNumber);

And i found the following:

export function fromString(pubMedID: Option<string>, doi: Option<string>, author: Option<string>, title: Option<string>, status: Option<string>, statusTermSourceREF: Option<string>, statusTermAccessionNumber: Option<string>, comments: Comment$[]): Publication {
    const status_1: OntologyAnnotation = new OntologyAnnotation(status, statusTermSourceREF, statusTermAccessionNumber);
    const pubMedID_1: Option<string> = map<string, string>(URIModule_fromString, pubMedID);
    const status_2: Option<OntologyAnnotation> = Option_fromValueWithDefault<OntologyAnnotation>(new OntologyAnnotation(), status_1);
    return Publication.make(pubMedID_1, doi, author, title, status_2, comments);
}

image

I tried to replicate this behavior on the Fable REPL, but the REPL transpiles it using unwrap (see REPL below).

Adding unwrap by hand to the typescript code seems to resolve the issue. Now i am not sure why this happens.

The F# code in question can be found: here

Repro code

REPL.

Expected and actual results

Please provide the expected and actual results.

Related information

  • Fable version: dotnet fable --version : 4.19.2
  • Operating system : Win 11 Pro
@ncave
Copy link
Collaborator

ncave commented Jun 14, 2024

@Freymaurer Yes, looks like it doesn't unwrap when you omit some of the optional parameters.
To reproduce, add another optional argument, but keep the caller the same, e.g.:

[<AttachMembersAttribute>]
type MyTestClass(?nameOption: string, ?otherOption: int) =
    member val Name = defaultArg nameOption "Kevin" with get, set

let testCreate (nameOption: string option) =
    MyTestClass(?nameOption = nameOption)

ncave added a commit to ncave/Fable that referenced this issue Jun 15, 2024
@ncave ncave mentioned this issue Jun 15, 2024
@ncave ncave closed this as completed in 914ab37 Jun 15, 2024
@HLWeil
Copy link

HLWeil commented Jun 17, 2024

Thank you for the quick fix, @ncave!

For when is the next release containing this fix planned?

@MangelMaxime
Copy link
Member

It is available in 4.19.3

@goswinr
Copy link
Contributor

goswinr commented Jun 18, 2024

@MangelMaxime @ncave I am also working on a library for both TS and .NET.
I have a lot of compile errors when generating d.ts definitions with TSC after fable compile to TS.
But so far I could just ignore them and use the generated definitions with the fable compiled JS. 😅
Is there any guide or example on how to best set up tsconfig.json for generating d.ts files with via fable and TSC ?

@Freymaurer
Copy link
Author

Have a look here, that is at least what worked for me 🙂 but i am by no means an expert for ts.

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

5 participants