-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
Use stronger types instead of String in ts type definitions. #9358
Comments
Looks like direction is defined using a union of string literal types: export type GridDirection = 'row' | 'row-reverse' | 'column' | 'column-reverse'; That means that the language service can provide suggestions, and it can complain if you specify an invalid direction. That's VS Code 1.18.1 with TypeScript version 2.6.1, but I think TypeScript introduced string literal types in 1.8. |
@espenh It is correct, but only after you've defined your initial string. |
I don't understand what you mean.
Isn't that true of the existing
The current |
That's not exactly true, from what I can gather string based enums are new in TypeScript, and heavily used where types are needed to be represented under a
Readability and discoverability improves tremendously as it's an indication that you can find more options under e.g <Grid container direction={'row'}> Doesn't tell me it's using a specific type, it tells me it's a Unfortunately I made the poor choice of using |
I don't know which editor you're using, but any editor using the TS language service has fine support for autocompletion and refactoring of string literal types. (Refactoring support isn't really relevant to this discussion though, because you as a user shouldn't be refactoring the names provided by
I'm well aware of string-based enums. I don't use them because they don't actually improve type correctness. What they do increase is verbosity, both at the definition and usage sites. I'd be interested to see an example where using an
If you type <Grid container direction=" and hit control-space in VSCode, it will give you dropdown of the possible completions. Again, I don't know what editor you're using, but you might want to look into alternatives. There are plenty that use the language service and understand that And why would you want to write <Grid container direction={GridDirection.Row} /> instead of just <Grid container direction="row" /> The latter is simple, clean, readable, free of redundancy and perfectly type safe.
Again, I'd really like to see a concrete example where |
One other important thing to consider is that |
I am using Verbosity sounds like a moot argument, since it's not really more verbose, then again coming from Obj-C, maybe I'm lenient on such things but I also read more than I write :) Again, when reading code that you or others wrote. <Grid container direction={GridDirection.Row} /> gives a stronger indication of values of a certain type allowed than <Grid container direction={'row'} /> I can also see what other approaches I have under It seems like I have to convince you why enums are better than union types and that's beyond what I was intending with this. From my point of view, using union types is wrong as it's was only a (temporary) hack because TypeScript was incomplete. I'd like to leave the hack behind and use the proper tools and types as I'd love better TypeScript support, for me its irrelevant wether or not the implementation layer is in JS. If it's not convincing enough, then I am sorry, I've explained as far as I can. You can read more about the reasoning here microsoft/TypeScript#3192 as much more involved people than me have figured out why it's necessary. Thank you for the discussion, feel free to close this. |
As far as readability, it's perfectly clear to me reading <Grid container direction="row" /> that If you command-click on As for union types being a "hack", I don't know what to tell you except that that's simply not accurate. They are a superior solution to the almost all the problems that |
Expected Behavior
I am expecting strong types for easier discovery through autocomplete when passing configurations to e.g
<Grid>
Current Behavior
Passing
String
as configurations to<Grid>
Context
From a developer UX point of view, discoverability is the key to making people comfortable and familiar with the large API. Thus offering a decent API is vital.
Would make things more stronger typed and easier to get help from autocomplete when filling in values at call site.
The text was updated successfully, but these errors were encountered: