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

It should be possible to generate TypeScript interfaces without "reserved" prefix for special keywords #1475

Closed
trevordixon opened this issue Aug 15, 2023 · 4 comments · Fixed by #1729
Labels
enhancement New feature or request stale

Comments

@trevordixon
Copy link

Instead of generating, for example reservedStatic, it could simply quote the property name:

interface PayloadFoo {
  'static': string;
  'status': string;
}

How hard is it to get this behavior right now?

@trevordixon trevordixon added the enhancement New feature or request label Aug 15, 2023
@jonaslagoni
Copy link
Member

jonaslagoni commented Aug 15, 2023

Super easy, there a couple of ways, this one is probably the best approach, overwriting constraint logic (ignoring reserved keywords) and then overwriting the default rendering of interfaces and the property to follow your syntax 'static': string;

const generator = new TypeScriptGenerator({
  constraints: {
    propertyKey: typeScriptDefaultPropertyKeyConstraints({
      // Ignore reserved keywords for properties entirely
      NO_RESERVED_KEYWORDS: (name) => { return name; }
    })
  },
  modelType: 'interface',
  defaultPreset: {
    interface: {
      property: ({property}) => {
        return `'${property.propertyName}': ${property.property.type};`
      }
    }
  }
});

Alternatively, a smaller but a little more hacky approach is just using the unconstrained property name as such:

const generator = new TypeScriptGenerator({
  modelType: 'interface',
  defaultPreset: {
    interface: {
      property: ({property}) => {
        return `'${property.unconstrainedPropertyName}': ${property.property.type};`
      }
    }
  }
});

And that's it 🙂

I wonder if it makes sense to make this the default outcome for interfaces? 🤔 Is there any downside you see @trevordixon?

@trevordixon
Copy link
Author

trevordixon commented Aug 16, 2023 via email

Copy link
Contributor

This issue has been automatically marked as stale because it has not had recent activity 😴

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience ❤️

@jonaslagoni
Copy link
Member

Solved in #1729

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request stale
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants