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
TypeScript: define string literal types for fixed-vocabulary fields #1624
Comments
Hi @tvald, We do use string literals in the TypeScript declaration files, but only when a shape is explicitly modeled as an enumeration in the model files shared by the AWS SDKs. For example, EC2's InstanceType shape is modeled as an enum |
I would like to see also union for region. // instead of
type AwsRegion = string;
// we could use
type AwsRegion = 'us-east-1' | 'eu-central-1' | 'eu-west-1' | ...; This could prevent errors like new AWS.Lambda({ region: 'us-eest-1' }); |
It seems the ts-generator appends a For example, this converts S3.BucketCannedACL to this I am trying to generate schemas automatically from the type-definitions, and this appendage is causing significant grief, requiring post processing. The underlying API call will not honor it, so not sure there is a legitimate purpose for it. Or am I missing something? Happy to open a new issue and submit a PR if that is the best course of action here. |
Greetings! We’re closing this issue because it has been open a long time and hasn’t been updated in a while and may not be getting the attention it deserves. We encourage you to check if this is still an issue in the latest release and if you find that this is still a problem, please feel free to comment or open a new issue. |
This seems like a simple enough fix and is an obvious bug IMHO: Will a PR be accepted for this? |
Would love to accept a PR |
This issue has not received a response in 1 week. If you still think there is a problem, please leave a comment to avoid the issue from automatically closing. |
oh you github action!!! |
@sramam, apologies there, my comment was added while I went through all the feature requests, since the response requested label was applied (which was just for the sake that a response was requested from you) actions did its job because it is set to 7 days, I agree with you with you about delayed response on this but didn't mean to close it. |
Not a problem - I wasn't complaining, just trying to be funny while staying pertinent. |
|
@SarthakC, that'd be really awesome I believe all that is needed is to convert This change is simple, but might result in the failure of some tests. I'm willing to help if you get stuck. |
Maybe it could be better to just use a |
Interesting idea @Shinigami92. |
You can still pass any string to a LiteralUnion but get autocompletion for known literals Hope we can add |
Unless I am misunderstanding something, isn't that the current implementation? When |
No, the current enum/literal don't provide autocompletion in your IDE! Also e.g. in tests you can find things like this: aws-sdk-js/test/region_config.spec.js Line 89 in 2f37893
|
@Shinigami92 I might mistaken in the previous comment.
If you put this snippet in the typescript playground, you'll see that option 2 is the right one - even per your comments from earlier in the thread. The base aws spec is an enum, but the typescript code generates a Union of StringLiterals. I'd still vote for: |
Mh yeah, if you name it |
IDE auto-completeThe lack of auto-complete in that case seems to be stemming from this, not the way the type is generated in the ts-generator. When defined properly, it'll work fine I think. I am not seeing the difference between the LiteralUnion as you referenced and TS default behavior when it's just a union of string literals. Test caseThe test case you pointed out is however an issue I imagine. |
|
What is a "custom region"? When are these valid? Looking at the code, the underlying schema specifies the type as an enum which is a non-extensible type in my understanding. For example here, how is I must be missing something really fundamental. Could you provide concrete examples of when this would be useful? [edit: fix markdown formatting] [edit2:] |
I personally don’t know any but for tests like the one above We could also build a |
Either it's strict or it's not. The specification being used by this generator is strict. I'd still vote for: -- The test case you pointed out seems to only instantiate an object for the signature without actually making any calls to AWS. I did not find any other instances with simple grep style searching - but the CI will report any misses. -- @SarthakC, hopefully this long discussion hasn't intimidated you. Eventually, I am also just a user of the library - the core team will make the decision to accept it or not. |
Any progress at all on this? It is really annoying not having the proper ENUM functionality on a typescript system for a strictly defined model |
Greetings! We’re closing this issue because it has been open a long time and hasn’t been updated in a while and may not be getting the attention it deserves. We encourage you to check if this is still an issue in the latest release and if you find that this is still a problem, please feel free to comment or open a new issue. |
Typescript supports string literal types, which are useful for constraining a field to a fixed set of valid string values.
Example:
The text was updated successfully, but these errors were encountered: