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

feat: generate type namespaces for enums as literals #960

Merged
merged 3 commits into from
Nov 2, 2023

Conversation

samualtnorman
Copy link
Contributor

this pull request adds code for generating a namespace of type declarations for enums as literals that let you use them like regular enums:
old:

let foo: typeof Foo.Bar

new:

let foo: Foo.Bar

this means a library that was previously using ts-proto without the enumsAsLiterals=true can add it with less breakages.


Foo.proto

enum Foo {
	Bar = 0;
	Baz = 1;
	Qux = 2;
}

old generated Foo.ts

export const Foo = { Bar: 0, Baz: 1, Qux: 2, UNRECOGNIZED: -1 } as const;

export type Foo = typeof Foo[keyof typeof Foo];

new generated Foo.ts

export const Foo = { Bar: 0, Baz: 1, Qux: 2, UNRECOGNIZED: -1 } as const;

export type Foo = typeof Foo[keyof typeof Foo];

export namespace Foo {
  export type Bar = typeof Foo.Bar;
  export type Baz = typeof Foo.Baz;
  export type Qux = typeof Foo.Qux;
  export type UNRECOGNIZED = typeof Foo.UNRECOGNIZED;
}

I've not looked into adding any tests or anything for this change, but if this feature is something you're interested in, I wouldn't mind adding tests (and making any other changes you request)

@stephenh
Copy link
Owner

stephenh commented Nov 1, 2023

Hi @samualtnorman . Thanks for the PR!

What you're trying to do makes sense, i.e. emulate the previous Foo.Bar syntax...I don't love using the namespace keyword, because awhile ago it wasn't supported in...some bundler...

But this is just for the type namespace, so I think it makes sense.

If you could find an integration-test/* directly that is using parameter.txt with enumsAsLiterals, and just maybe update an existing test/file in their to show your output is getting generated as you like, and then also be a regression test, that'd be great.

Thanks!

@samualtnorman
Copy link
Contributor Author

what would the regression test be testing for?

@stephenh
Copy link
Owner

stephenh commented Nov 2, 2023

I was thinking just something in a test file that did someFunction(value: Foo.Bar) to show the Foo.Bar syntax working, but I think the checked-in generated output should be a good enough regression test. Thanks!

@stephenh stephenh merged commit e2619f6 into stephenh:main Nov 2, 2023
6 checks passed
stephenh pushed a commit that referenced this pull request Nov 2, 2023
# [1.163.0](v1.162.2...v1.163.0) (2023-11-02)

### Features

* generate type namespaces for enums as literals ([#960](#960)) ([e2619f6](e2619f6))
@stephenh
Copy link
Owner

stephenh commented Nov 2, 2023

🎉 This PR is included in version 1.163.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@samualtnorman
Copy link
Contributor Author

thank you for accepting my change!

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

Successfully merging this pull request may close these issues.

None yet

2 participants