Json property alias in typescript

Viewed 8936

Let's say I want to get a data from Visual Studio TFS and the response (as json) is in this kind of format:

{
    "Microsoft.VSTS.Scheduling.StoryPoints": 3.0,
    // ......
}

There's dot in the property name. Reading from other questions I found out that I can read that json in typescript by using an interface like this

export interface IStory { // I don't think this kind of interface do me any help
    "Microsoft.VSTS.Scheduling.StoryPoints": number
}

And then I can use the property with this syntax:

var story = GetStoryFromTFS();
console.log(story["Microsoft.VSTS.Scheduling.StoryPoints"]);

But I'd prefer not to call the property like this, since the intellisense won't able to help me finding which property I want to use (because I call the property using a string).

In C# there is a JsonProperty attribute which enable me to create a model like this:

public class Story
{
    [JsonProperty(PropertyName = "Microsoft.VSTS.Scheduling.StoryPoints")]
    public double StoryPoints { get; set; }
}

And then I can use the property this way:

var story = GetStoryFromTFS();
Console.WriteLine(story.StoryPoints);

This way the intellisense will able to help me finding which property I want to use.

Is there something like JsonProperty attribute in typescript? Or is there any other, better way, to achieve this in typescript?

1 Answers
Related