/TypeScriptDefinitionGenerator

A Visual Studio extension

Primary LanguageC#OtherNOASSERTION

TypeScript Definition Generator

Build status

NOTE: This is a customed redistribute

The orginal repo wrote by @madskristensen, best regard for him!

Download this extension from the Marketplace or get the CI build.


Creates and synchronizes TypeScript Definition files (d.ts) from C# model classes(DTO) to build strongly typed web application where the server and client-side models are in sync. Works on all .NET project types

See the change log for changes and road map.

For Visual Studio 2015 support, install Web Essentials 2015

From C# to .d.ts file

This extension will automatically generate .d.ts files from any C# file you specify. It will turn the following C# class into a TypeScript interface.

For *.cs file like this

namespace foo
{
    public class ReuqestBase
    {
        public string RequestName { get; set; }
    }

    public class CustomerReuqest : ReuqestBase
    {
        public CustomerDto CustomerDto { get; set; }
    }

    /// <summary>
    /// dto for single customer
    /// </summary>
    public class CustomerDto
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public IEnumerable<string> Tags { get; set; }
        //predefined type for customers
        public CustomerType CusomerType { get; set; }
    }

    public enum CustomerType
    {
        Normal = 0,
        /// <summary>
        /// Charged over $100
        /// </summary>
        Vip = 1
    }
}

Becomes this .d.ts file:

declare module Server.Dtos {
	interface ReuqestBase {
		requestName: string;
	}
	interface CustomerReuqest extends ReuqestBase {
		customerDto: Server.Dtos.CustomerDto;
	}
	/** dto for single customer */
	interface CustomerDto {
		id: number;
		name: string;
		tags: string[];
		/** predefined type for customers */
		cusomerType: Server.Dtos.CustomerType;
	}
	const enum CustomerType {
		Normal = 0,
		/** Charged over $100 */
		Vip = 1,
	}
}

The generated .d.ts file can then be consumed from your own TypeScript files by referencing Server.Dtos.<typeName> interfaces.

Generate d.ts file from C#

To generate a .d.ts file, right-click any .cs file and select Generate TypeScript Definition.

Then a .generated.d.ts file is created and nested under the parent C# file.

Context menu

Every time the C# file is modified and saved, the content of the .d.ts file is updated to reflect the changes.

Settings

Configure this extension from the Tools -> Options -> Text Editor -> JavaScript/TypeScript -> Generate d.ts dialog.

Settings

Contribute

Check out the contribution guidelines if you want to contribute to this project.

For cloning and building this project yourself, make sure to install the Extensibility Tools 2015 extension for Visual Studio which enables some features used by this project.