Closed andry-tino closed 6 years ago
Why not emit something like
entities: { [id: T]: V}
where T is the Key type and V is the value type?
@michaelaird: I see. It is actually better probably. However, say I have this in Scriptsharp:
public class MyClass {
public void DoSome(Dictionary dict) { /* Some code */ }
}
So in Typescript I should render it as:
export class MyClass {
doSome(dict: [id: T]: V): void;
}
Is it correct? Since I am not so familiar with generics in TypeScript, I think I need to declare T
and V
somewhere? If so then it becomes more complicated and it would call for actually starting integrating support to generics in Rosetta; something which is scheduled for later.
So if the syntax does allows types not to be declared ok, but if i need to do something like: doSome<T,V>(dict: [id: T]: V)
, then I will not go for that sorry... Not in scope now.
strong typing FTW!
So, it seems like I need to declare those types: Generics in Typescript. It will be for later man, I am sorry. But maybe I can do something like:
[id: string]: any
In the case of ScriptSharp, dictionaries accept only strings, so it should be ok.
Sorry!
My example didn't mean to use generics. I believe you should know the key/value type at the time when you're generating code so you can emit the full type.
Exactly as you say , System.Collections.Dictionary should emit
[id: string]: any
The System.Collections.Generic.Dictionary<TKey, TValue> should emit
[id: TKey]: TValue
but the emitted TypeScript should have the the actual types that are declared for TKey and TValue.
When I generate the dictionary with that syntax, something is wrong:
declare module MyNamespace{
export class MyClass{
public attributes : [id: string]: any; // <== Complains that `id` is not defined
}
}
What is the correct syntax to define a dictionary type in TypeScript?
I see now... it should be generated as:
declare module MyNamespace{
export class MyClass{
public [attributes: string]: any;
}
}
Which makes the feature more difficult to build eheh :) I will timebox this as I need this ASAP, so if I cannot find a suitable design for this in short time, I will just emit any
for now.
Ok, so I researched more and it looks like this is the correct way:
declare module MyNamespace{
export class MyClass{
public attributes: { [id: string]: any };
}
}
Which I can do :)
At the moment, we emit type:
As is. While we should emit it as:
Important A flag should be used to activate/deactivate this mapping.