-
Notifications
You must be signed in to change notification settings - Fork 38
Data Mapping with TypeScript
The following code snippet may illustrate the mapping between .NET simple types and TypeScript types.
static readonly Dictionary<string, string> typeMap = new Dictionary<string, string>()
{
{typeof(int).FullName, "number"},
{typeof(uint).FullName, "number"},
{typeof(long).FullName, "number"},
{typeof(ulong).FullName, "number"},
{typeof(short).FullName, "number"},
{typeof(ushort).FullName, "number"},
{typeof(float).FullName, "number"},
{typeof(double).FullName, "number"},
{typeof(decimal).FullName, "number"},
{typeof(byte).FullName, "number"},
{typeof(sbyte).FullName, "number"},
{typeof(string).FullName, "string"},
{typeof(char).FullName, "string"},
{typeof(Guid).FullName, "string"},
{typeof(bool).FullName, "boolean"},
{typeof(void).FullName, "void"},
{typeof(object).FullName, "any"},
{typeof(DateTime).FullName, "Date"},
{typeof(DateTimeOffset).FullName, "Date"},
//TimeSpan is not so supported in Javascript
};
In addition to CLR array, the types illustrated in the following code snippet are translated into TypeScript array.
static readonly System.Collections.Generic.HashSet<string> arrayTypeNames = new System.Collections.Generic.HashSet<string>(
new string[]() {
typeof(IEnumerable<>).FullName,
typeof(IList<>).FullName,
typeof(ICollection<>).FullName,
typeof(IQueryable<>).FullName,
typeof(IReadOnlyList<>).FullName,
typeof(List<>).FullName,
typeof(System.Collections.ObjectModel.Collection<>).FullName,
typeof(IReadOnlyCollection<>).FullName
}
);
Hints:
Since Javascript supports only single dimensional array, this article "Tricky Array" may help you to deal with multidimensional array.
A dictionary is based on a hash table available only at run time, and it is generally impossible to transport a hash table over wire. However, common practices are to transport the key / value pairs in a list, and it is up to the client programs to decide whether to reassemble the list into a dictionary, or just use the list through indexer.
IDictionary<K,V> and Dictionary<K,V> are translated into Dictionary<ClientK, ClientV>.
Remarks:
Such transportation and transformation are done by Newtonsoft.JSON which can reassemble the key / value pairs into a dictionary.
IDictionary<K,V> and Dictionary<K,V> are translated into an indexer like
{[id: string]: DemoWebApi_DemoData_Client.Person }
KeyValuePair<K, V> is translated into a structure like
{Key: string, Value: DemoWebApi_DemoData_Client.Person }
Remarks:
In Javascript, the key of an indexer must be string.
By default, all members generated in a TypeScript interface are optional, unless the members in the .NET classes are decorated with one of the following:
[DataMember(IsRequired =true)]
...
[JsonProperty(Required = Required.Always)]
...
[Required]
...