-
Notifications
You must be signed in to change notification settings - Fork 15
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
Replace const enum in .d.ts with string literal union #15
Comments
Could you please share the EDIT: actually, upon looking at this again, I realised that on my side the syntax you show also doesn't get a type error - but it does result in a runtime error:
I'll have to check if this doesn't have anything to do with Bun doing something weird re TypeScript. But could you please check if the code you show actually runs without an error? If yes, then I'd still be curious to see the configuration. |
i haven't checked, but it probably doesnt compile :) i'm fine with the proposed change, since the strings are not int enum values and not completely nonsensical, and this doesnt create the mess that a regular enum would create at build time, though that may be irrelevant for a js lib where this is just a type declaration and the lib itself does not use it anyways. |
(merged #16) |
Thanks for the great library!
I'm having an issue related to #6 after using the suggested fix to #14
The problem is that importing const enum from a .d.ts file doesn't really work properly.
Consider the following fix to #14:
This results in a TS error:
You can silence the error by forcing a cast to
SchemaColumnType
like this:but this is not recommended because it essentially removes the type checking, and if
SchemaColumnType
changes, or someone passes a wrongtype
value, TypeScript will not pick up the error:I would suggest removing the const enum from the .d.ts and replacing it with a string literal union, which I think is the correct way to provide good DX here. Like this (I'll submit this exact suggestion as a PR):
This will also give autocomplete:
Now, the only bit where this is not perfect is that the single-character strings are a bit less obvious than the names of the const enum elements. And currently, TypeScript sadly does not pick up the comments next to the union elements. But once it does (microsoft/TypeScript#38106), the comments should be included in IntelliSense.
But anyway, considering that the const enum approach is either incorrect or dangerous (depending on how you look at it), I think this is still a better alternative.
The text was updated successfully, but these errors were encountered: