ReinforcedTypings RtDictionaryType 替换

ReinforcedTypings RtDictionaryType substitution

我尝试在我的模型中使用 Microsoft.FSharp.Collections.FSharpMap(F# 中的标准地图)。 它实现了

System.Collections.Generic.IReadOnlyDictionary<TKey, TValue> 

System.Collections.Generic.IReadOnlyCollection<System.Collections.Generic.KeyValuePair<TKey, TValue>>

所以是字典类型。 不幸的是,ReinforcedTypings 给了我这个警告:

Type resolvation warning RT0003: Could not find suitable TypeScript type for System.Collections.Generic.KeyValuePair`2[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[BkkFutar.Data.BKKApi.RouteReference, BkkFutarCore.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. 'any' assumed

它看起来像是识别为 IReadOnlyCollection> 而不是字典。

我也试过用

Substitute(typeof(FSharpMap<,>),` new RtDictionaryType())

在配置中,但它不起作用。 另一方面

Substitute(typeof(FSharpMap<string,string>), new RtDictionaryType(new RtSimpleTypeName("string"), new RtSimpleTypeName("string")))

确实有效,但我不想也不能指定每个案例。

您必须改用 SubstituteGeneric

SubstituteGeneric(typeof(FSharpMap<,>), (t, tr) =>
{
    var args = t.GetGenericArguments();
    return new RtDictionaryType(tr.ResolveTypeName(args[0]),tr.ResolveTypeName(args[1]));
})

我专门为你做了相应的unit test。 :)

这似乎是内部 RT 的...不便之处,本身并不是错误。 RT 首先通过实现的 IDictionary<,> 接口检测字典,然后,如果不成功,它会尝试在类型中找到 IEnumerables。这就是为什么你收到关于 KeyValuePair<,> 的警告:FSharpMap 实现了其中的 IEnumerable<>

IDictionary<,> 接口实际上代表包含 Add 方法的可变 hashmap。 IReadOnlyDictionary 在 .NET 中的出现比 RT 的早期版本稍晚。

顺便说一下,将 IReadOnlyDictionary 解析为 TypeScript 的哈希对象是不正确的,因为这个对象实际上是可变的。但无论如何,我会考虑在下一版本的 RT 中进行此改进。