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] IDictionary and IReadOnlyDictionary interfaces #3665

Open
leononame opened this issue Dec 14, 2023 · 2 comments
Open

[TypeScript] IDictionary and IReadOnlyDictionary interfaces #3665

leononame opened this issue Dec 14, 2023 · 2 comments

Comments

@leononame
Copy link

Description

I understand it's not part of the F# core library and isn't covered in the docs either, but IReadOnlyDictionary and IDictionary are abstractions around types that are covered by Fale (Dictionary and Map) and are well integrated with F# using dict and readOnlyDict. In TypeScript, the types compile to any, which makes it impossible to use it under certain circumstances.

Repro code

open System.Collections.Generic

type T = {A: int }
// Write code or load a sample from sidebar
let dict = [(1,{A=1});(2,{A=1});(3,{A=1})] |> readOnlyDict
let getValue (dict: IReadOnlyDictionary<int, T>) value = 
    dict[value].A

let value = getValue dict 1

results in this TypeScript code:

import { Record } from "fable-library/Types.js";
import { int32 } from "fable-library/Int32.js";
import { IComparable, IEquatable } from "fable-library/Util.js";
import { record_type, int32_type, TypeInfo } from "fable-library/Reflection.js";
import { getItemFromDict } from "fable-library/MapUtil.js";

export class T extends Record implements IEquatable<T>, IComparable<T> {
    readonly A: int32;
    constructor(A: int32) {
        super();
        this.A = (A | 0);
    }
}

export function T_$reflection(): TypeInfo {
    return record_type("Test.T", [], T, () => [["A", int32_type]]);
}

export const dict: any = new Map<int32, T>([[1, new T(1)] as [int32, T], [2, new T(1)] as [int32, T], [3, new T(1)] as [int32, T]]);

export function getValue(dict_1: any, value_1: int32): int32 {
    return getItemFromDict(dict_1, value_1).A;
}

export const value: int32 = getValue(dict, 1);

However, return getItemFromDict(dict_1, value_1).A; is an error in TypeScript (TS2571: Object is of type unknown). I understand that IDictionary is more complicated because it has a bigger surface, but compiling IReadOnlyDicitionary to something similar to what IMap is in TypeScript should be possible, right?

@dbrattli
Copy link
Collaborator

Thanks for opening this issue. I think this should be possible. We have those interfaces for Python (almost) so should be possible for TypeScript as well. Let me have a look ...

@leononame
Copy link
Author

@dbrattli Hi, sorry for bothering again. Did you have any success, it's kind of a blocking issue for me at the moment. I'd be happy to help out as well if I can, but maybe you could give me a pointer on where to start?

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

2 participants