Currently, I am in the process of transitioning a C# desktop application to an Angular/TypeScript web application.
In the C# application, all class properties are named using PascalCase. Therefore, I decided to maintain this naming convention in the TypeScript classes as well.
Below are examples of two similar TypeScript classes, with one using PascalCase and the other camelCase:
//PascalCase
export class Info
{
public ManagedType:string;
public ApiTemplate:string;
}
//camelCase
export class Info
{
public managedType:string;
public apiTemplate:string;
}
Now, let's discuss the peculiar behavior that I have observed:
When retrieving JSON data from the server and populating an array of the Info class, the naming convention (PascalCase or camelCase) used in the TypeScript class does not affect the data retrieval process.
this.Infos = await this.HttpClient.get<Info[]>(this.Url).toPromise<Info[]>();
However, upon logging the array to the console, I noticed that the output consistently uses camelCase for the properties, regardless of whether the Info class uses PascalCase or camelCase.
Here is where things get interesting: When attempting to filter the array to retrieve a specific instance of the Info class using PascalCase, the result is always undefined/null.
On the contrary, when filtering the array using camelCase, the specific instance of the Info class is successfully found and returned.
//This approach results in 'Info' being undefined, even though the array exists. let Info = Infos.filter(i => i.ManagedType == "Something" && i.ApiTemplate == "Something else")[0]; //In contrast, this method correctly retrieves 'Info'. let Info = Infos.filter(i => i.managedType == "Something" && i.apiTemplate == "Something else")[0];
My queries are:
Why does this discrepancy exist? Is it a TypeScript limitation or an issue specific to Angular?
Is there an implicit convention that I need to adhere to?
And why doesn't the TypeScript compiler warn or throw an error regarding the potential inconsistencies when using PascalCase?