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

getElementById is unsafe #99

Open
alex35mil opened this issue Jun 24, 2022 · 2 comments
Open

getElementById is unsafe #99

alex35mil opened this issue Jun 24, 2022 · 2 comments

Comments

@alex35mil
Copy link

Hi, I was trying out F# recently and encountered a runtime error when using getElementById due to the return type being HTMLElement but not option<HTMLElement>.

@alfonsogarciacaro
Copy link
Member

Thanks for pointing this out. We had this discussion before (I forgot where so I don't have a link, sorry), but the conclusion was in general we're not using option to represent anything that can be null in JS. One of the reasons is that when we converted the fable-browser bindings from .d.ts declarations, Typescript wasn't "null safe" yet, so it was difficult to know which APIs could return null values without checking one by one. Maybe we could try to do it now but it may cause several breaking changes.

For now you can just do a null check as you would do in JS, either with isNull op pattern maching:

match document.getElementById "my-id" with
| null -> // do something
| el -> // do something else

@kerams
Copy link
Contributor

kerams commented May 26, 2023

Once dotnet/fsharp#15181 lands, we should be able to indicate nullability in bindings. Hopefully the latest compiler and lang version switch will be enough (both on the side of this repo and user projects).

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