Imagine I need to fetch data from Visual Studio TFS
and the response (in json format) looks like this:
{
"Microsoft.VSTS.Scheduling.StoryPoints": 3.0,
// ......
}
The property names have dots. After researching, I learned in TypeScript I can create an interface like this:
export interface IStory { // This interface might not be very helpful
"Microsoft.VSTS.Scheduling.StoryPoints": number
}
To access the property, I could use the following syntax:
var story = GetStoryFromTFS();
console.log(story["Microsoft.VSTS.Scheduling.StoryPoints"]);
However, I'd rather not access the property this way because it won't provide suggestions through intellisense due to using a string key.
In C#, there is a JsonProperty
attribute to define a model like this:
public class Story
{
[JsonProperty(PropertyName = "Microsoft.VSTS.Scheduling.StoryPoints")]
public double StoryPoints { get; set; }
}
This allows me to access the property as follows:
var story = GetStoryFromTFS();
Console.WriteLine(story.StoryPoints);
With this approach, intellisense can assist in selecting the desired property.
Is there an equivalent of JsonProperty
attribute in TypeScript? Or perhaps a better method to achieve this functionality in TypeScript?