-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Support inlining strings/const enum string types #3922
Comments
@weswigham so using like export enum ErrorHint {
ERROR_HINT_DEFAULT = <any>'',
ERROR_HINT_NOTAUTHORIZED = <any>'not_authorized', // unauthorized
ERROR_HINT_SERVERERROR = <any>'server_error',
} should not be used in any case since it is an error at runtime or - better - it is a side effect? |
With what we currently have in master, you can do this, instead: export namespace ErrorHint {
export const ERROR_HINT_DEFAULT: '' = '';
export type ERROR_HINT_DEFAULT = '';
export const ERROR_HINT_NOTAUTHORIZED: 'not_authorized' = 'not_authorized';
export type ERROR_HINT_NOTAUTHORIZED = 'not_authorized';
export const ERROR_HINT_SERVERERROR: 'server_error' = 'server_error';
export type ERROR_HINT_SERVERERROR = 'server_error';
}
export type ErrorHint = ErrorHint.ERROR_HINT_DEFAULT | ErrorHint.ERROR_HINT_NOTAUTHORIZED | ErrorHint.ERROR_HINT_SERVERERROR; It's a bit verbose/repetitive, but it works really well, thanks to string literal types. (And looks/functions just like you'd expect an enum of strings to) |
ah so like the example I was looking at! namespace S {
export const A: "A" = "A";
export type A = "A";
export const B: "X" = "X";
export type B = "X";
export const C: "C" = "C";
export type C = "C";
// Imagine this were possible
[s: string]: S;
}
type S = S.A | S.B | S.C; without the ok thank you it's ok for now! |
@weswigham @loretoparisi Correct me if I am wrong, but this:
cannot be used in a declaration file, because unlike regular |
Eg, we should allow
and compile it as we do numerical const enums.
Right now static classes end up getting used for this and don't get typechecked as an enum type - using an string enum clarifies types more, inlining the string clarifies the outputted JS more, and the feature in general could make writing
.d.ts
files for libs with event names or magic strings much easier. For example,String.normalize
is currently defined as:We could get better intellisense and clearer, more self-documenting code with something like so:
It could also be worth discussing allowing enums in general to be based on arbitrary base types (or at least string/symbol based types, since those are valid object keys), similar to how enums can be based on any numeric type in C#.
Also, in case anyone wasn't aware, for non-const string enums we already emit (mostly) correct code, despite it (technically) being an error to do so - the following compiles without error:
to
without the
<any>
casts its a type error to do so, however. (And it's pretty simple as to why you probably shouldn't do this - you can corrupt the enum like so):Foo.A has value "C" rather than "0" in this scenario, thanks to how the emit for enums currently works. However, with const enums this should never be an issue, so I believe that we should start with allowing string const enums.
The text was updated successfully, but these errors were encountered: