Michelle
Michelle

Reputation: 537

Using Web Essentials 2013 to generate typescript intellisense file not working

I have visual studio 2013 installed with web essentials 2013. I right-click a .cs file and select "Web Essentials -> Create Typescript intellisense file", such that I get a typescript d.ts file that I can reference in the typescript code. I ran into a few issues with this. Let's say my class is as follows:

namespace SomeNamespace
{
    [TypescriptModule("test")]

    [DataContract(Name = "MyDTO")]
    public partial class MyPM
    {
        [DataMember(Name = "id")]
        public long Id { get; set; }

        [Required]
        [DataMember(Name = "name")]
        public CustomPM Data{ get; set; }
    }
}

The d.ts file I get is:

declare module test {
    interface MyPM{
        id: number;
        data: test.CustomPM;
    }
}

The first problem is that the line:

[DataContract(Name = "MyDTO")]

is being ignored during generation. It is not generating a class called MyDTO, but a class called MyPM, even though MyDTO is the string specified for use during serialization and that's what goes over the wire to the client. I did some investigation and the code here:

https://github.com/madskristensen/WebEssentials2013/blob/master/EditorExtensions/Commands/Code/IntellisenseParser.cs#L207

indicates that it uses CodeClass.Name property to determine the class name. Is there a way to use attributes on the class (something besides DataContract Name) to make the CodeClass.Name property contain another string besides the actual class name?

The second problem is this line:

data: test.CustomPM;

In visual studio 2013 with typescript installed, test.CustomPM gives me an error because it is unable to find the type CustomPM in the test module, even though the class has been generated into the same "test" module and is available for use. The part that is missing is this line at the top of the d.ts file:

/// <reference path="./CustomPM.cs.d.ts"/>

However, I have been unable to find a way to have the generator generate this as well.

I would really appreciate is anyone can shed some light on ways to work around these issues. Thanks!

Upvotes: 5

Views: 1800

Answers (2)

user1641172
user1641172

Reputation:

You can reference your generated types in your base tsconfig.json, then you don't need the custom reference in every typescript file:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "jsx": "preserve"
  },
  "files": [
    "../typings/index.d.ts",
    "./TestForm.tsx",
    "./declaration.d.ts",
    "../models/models.cs.d.ts"
  ]
}

Upvotes: 0

antoinestv
antoinestv

Reputation: 3306

It is an old question, and I will not answer directly because I didn't know this fonctionnality of WebEssentials.

But i will give you an alternative that works really well :

TypeLite - http://type.litesolutions.net/

You just have to put the attribut [TsClass] on the classes that you want.

If you want your TS interface to start with I and all properties camelCase, edit TypeLite.Net4.tt :

 var ts = TypeScript.Definitions()
        .WithReference("Enums.ts")
        .ForLoadedAssemblies()
        .WithFormatter((type, f) => "I" + ((TypeLite.TsModels.TsClass)type).Name) 
        .WithMemberFormatter((TypeLite.TsModels.TsProperty identifier) => Char.ToLower(identifier.Name[0]) + identifier.Name.Substring(1)); 

Upvotes: 0

Related Questions