andry-tino / Rosetta

Toolset for migrating your codebase from C# to TypeScript
http://antino-enlad.cloudapp.net:8080/job/Rosetta/
GNU General Public License v3.0
24 stars 9 forks source link

Emit System.Collections.Dictionary as any #54

Closed andry-tino closed 6 years ago

andry-tino commented 6 years ago

At the moment, we emit type:

`System.Collections.Dictionary`

As is. While we should emit it as:

any

Important A flag should be used to activate/deactivate this mapping.

michaelaird commented 6 years ago

Why not emit something like

entities: { [id: T]: V}

where T is the Key type and V is the value type?

andry-tino commented 6 years ago

@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.

michaelaird commented 6 years ago

strong typing FTW!

andry-tino commented 6 years ago

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.

michaelaird commented 6 years ago

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.

andry-tino commented 6 years ago

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?

andry-tino commented 6 years ago

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.

andry-tino commented 6 years ago

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 :)