Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add import path support for CustomTypeMappings #198

Open
mike8161990 opened this issue Apr 9, 2024 · 1 comment
Open

Add import path support for CustomTypeMappings #198

mike8161990 opened this issue Apr 9, 2024 · 1 comment

Comments

@mike8161990
Copy link

mike8161990 commented Apr 9, 2024

In my team's project, we prefer to map all DateTime and DateTimeOffset C# types to a Moment TypeScript type. Using this type requires an additional import from the moment package.

I found that I am able to do this for individual fields using a GenerationSpec:

public class DemoGenerationSpec : GenerationSpec
{
    public override void OnBeforeGeneration(OnBeforeGenerationArgs args)
    {
        this.AddInterface<UserDetailed>()
            .Member(t => nameof(t.CreatedOn))
            .Type("Moment", "moment");
    }
}

Or using an annotation:

public class UserDetailed
{
    [TsType("Moment", "moment")]
    public required DateTimeOffset CreatedOn { get; set; }

    // Trimmed for brevity
}

But I can't find a way to setup a universal type mapping rule which will affect all usage of a type. Does such a method exist?

Issue #175 seems to touch on this a bit noting that the CustomTypeMappings option could be changed from a IDictionary<string, string> to some kind of object allowing additional configuration. Maybe this would be a good use case for that restructuring?

@janruo
Copy link

janruo commented Jul 2, 2024

+1 This would be very useful.

In the meantime, config can be used to map the type, and also inject its import to all generated files:

{
    "customTypeMappings": {
        "Some.Custom.Type": "SCT"
    },
    "fileHeading": "/** This is an auto-generated file */\r\n\r\nimport { SCT } from \"somewhere\";\r\n"
}

The downside is of course that it will be added to ALL generated files, even the ones that don't need it. But /* eslint-disable */ could also be injected to supress warnings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants