svenheden / csharp-models-to-typescript

C# models to TypeScript
89 stars 58 forks source link

Underscores are not handled correctly as attributes #66

Open sahin52 opened 1 year ago

sahin52 commented 1 year ago
public class Testing
{
        public int Success { get; set; }
        public int GU_ID { get; set; }
        public int GUID { get; set; }
        public int G_UID { get; set; }
        public int _GUID { get; set; }
        public int GUID_ { get; set; }
}
public enum TestEnum
{
        GU_ID,
        _GUID,
        GUID,
        GUID_
}

is converted to this:

export interface Testing {
        success: number;
        guid: number;
        guid: number;
        gUID: number;
        guid: number;
        guid: number;
    }
    export enum TestEnum {
        GU_ID = 0,
        _GUID = 1,
        guid = 2,
        GUID_ = 3,
    }

As you see, the underscore is not correctly handled in class attributes, it is removed, but it is correctly handled in enums. my config is like this:

{
    "include": [
        ".\\BO\\**\\*.cs"
    ],
    "exclude": [
    ],
    "namespace": "\"ApiTypes\"",
    "output": "types.d.ts",
    "camelCase": true,
    "camelCaseEnums": false,
    "camelCaseOptions": {
        "pascalCase": false,
        "preserveConsecutiveUppercase": true,
        "locale": "en-US"
    },
    "numericEnums": true,
    "stringLiteralTypesInsteadOfEnums": false,
    "customTypeTranslations": {
        "char": "string",
        "Exception":"any"
    }
}

I'd love to try to solve this problem, if you would merge it to the project.

sahin52 commented 1 year ago

Oops, after research, I saw that it is related to camelCase option. But there is no option to not remove underscore in camelCase. But if I don't use camelCase, the generated types will start with upper case. I may close this issue, but I still want a way to make attributes start with lower case and preserve the underscore.

sahin52 commented 1 year ago

Created this pull request: https://github.com/svenheden/csharp-models-to-typescript/pull/67 which will solve this problem. If you have the same problem, you can add a config like this after using this pull request:

{
    "lowerFirstLetters": true,
    "include": [
        "C:\\X\\Y\\Z\\**\\*.cs"
    ],
    "exclude": [
    ],
    "namespace": "\"Api\"",
    "output": ".\\types.d.ts",
    "camelCase": false,
    "camelCaseEnums": true,
    "numericEnums": true,
    "stringLiteralTypesInsteadOfEnums": false,
    "customTypeTranslations": {
        "char": "string"
    }
}