Standardize readme type notation #1
Replies: 14 comments
-
|
All conventions sound good to me. As for the last one:
I'm voting for |
Beta Was this translation helpful? Give feedback.
-
But there is a difference between
That's true, but the API docs should be correct as to what it returns. If we write About the So these are some examples that I might use in documentation
I feel But if we really want to go for consistency, we should use the |
Beta Was this translation helpful? Give feedback.
-
|
So some things I'm seeing are not very Javascript-y. One must look at the practicality of basic types, for example. Nobody uses boxed types. That's the reality of it. If you're using boxed types, there's usually another way of doing it - especially at the API level. As far as collections go, I agree with the sentiment that It seems such declarations are trying to make things look a lot like code and a lot less like human-readable documentation. While I understand the need for such a standardization to be regular so it can be parsed automatically, it should still be parsable manually. One thing to mention, as someone who finds verbosity in, say, Java a little overwhelming at times, is when you need to nest collections. What about nests of nests? What about an unbalanced nesting?
There are other things to consider here as well. Optional/multi-type arguments, weak contract types (i.e. something that is an Yet another consideration is to show the intent of the object. Since Javascript is prototypical and allows for some very clever hacks, if we show how the value is to be used, the consuming library can make the we're-all-consenting-adults-here decision to (mis)use it in such a way that works for them. That's one of the beauties of dynamically typed languages, and why Javascript is seen as powerful in the first place. The best part is, if the consuming library is misusing a type that has been well-defined insofar as the intent, the repository owner doesn't have to support it and can give clear reasons as to why (i.e. the docs tell you exactly how it's supposed to be used). We can't control downstream consumers, so we might as well protect ourselves with docs. At the very least, I highly suggest we keep with Javascript object notation for plain objects. is much easier to read and understand than Further, I am throwing my hat in the ring for wrapping array types with square brackets (though I do not have a strong opinion on this either way). As for multiple acceptable types, a simple When it comes to constant values, those should be capitalized. Since, as I suggested earlier, boxed types shouldn't be used at an API level, this makes constant values completely discernible. If the function is going to be using the passed array in a way that requires a real array, I think it should be denoted as such. On the contrary, if we want to imply all arrays are to be strict (i.e. or something to that effect - depending on how the syntax is formulated. When it comes to sets (which I don't personally believe warrant their own 'primitive' type in this documentation), we could follow suit to strict arrays: and bonus points for combining them (can't really do it with the first example from above): Another alternative for collections would be slightly more cryptic but more terse - in that you'd have to know what you're looking for. For most cases this is innocuous as most developers tend to know when they're passing a pseudo array when they should be passing a real array given an inevitable error. However, to document it better, we could be using ellipsis to show collections and use angle brackets to denote strict array requirements. There a few benefits to this approach:
Just some thoughts. The goal here is to be readable and clear for both correctness as well as intent. I think pulling in features of other languages just to describe this language is a bit foolish. Javascript isn't type safe and will never be type safe, and trying to box it into being type safe, in my opinion, is a cause for writing less than hardy code (at least as far as Node.js goes). |
Beta Was this translation helpful? Give feedback.
-
|
That's quite some content to process :). My first remark I have is regarding this piece
You might be right that it's much easier to reason about, but we're talking about an ES6 Map type here. That's far from equivalent to |
Beta Was this translation helpful? Give feedback.
-
|
I'm not a fan of annotations that cannot be statically verified. With the default PureScript tooling for example, you get your type docs automatically generated and updated on every build. But in any case I'd avoid creating another standard to document types. With TypeScript you could keep your "typings" separate and have a clear, standardized syntax for describing them. What about that approach? |
Beta Was this translation helpful? Give feedback.
-
|
I don't have time to react to everything said here (hopefully later today). But a few quick thoughts:
|
Beta Was this translation helpful? Give feedback.
-
|
Also worth mentioning that documentation.js / esdoc both seem to favor |
Beta Was this translation helpful? Give feedback.
-
|
Found this in the esdoc documentation /**
* @param {number[]} param - this is array param.
*/
function myFunc(param){...} |
Beta Was this translation helpful? Give feedback.
-
|
@sindresorhus Sorry to jump in here!
So to answer your question "If an Array can contain multiple types", you would want to use |
Beta Was this translation helpful? Give feedback.
-
|
Note to self: I'm just gonna use TypeScript notation. That means all primitives are lowercase and non-primitives are pascal-case. In addition, I'm gonna start using And |
Beta Was this translation helpful? Give feedback.
-
|
Slightly related, I'm going to start using |
Beta Was this translation helpful? Give feedback.
-
|
Aren’t linebreaks added with double spaces at the end of a line? |
Beta Was this translation helpful? Give feedback.
-
|
Not possible with trim trailing whitespace set :) |
Beta Was this translation helpful? Give feedback.
-
|
@fregante I used to use double-spaces, people kept stripping them in pull requests. Double-spaces was a horrible idea anyway. Glad we have |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to standardize the type notation I use in my API docs. Currently I'm using
string,array, etc, but I have no consistent notation for a collection of values and I usebuffer, which is clearly wrong as the type isBuffer.Example: https://github.com/sindresorhus/boxen#borderstyle
I would like to figure out a good convention for this.
My proposal is to follow the Facebook Flow types as close a practical.
One thing I don't like with the Flow types is that primitives are lowercase. While this makes sense in typing, it looks very ugly in API docs:
I propose we make an exception and also PascalCase primitives.
We also need a notation for nested types. For example an Array of booleans or a Set of strings.
TypeScript (and Java) seems to use
string[]for an array of strings. While Facebook Flow usesArray<string>. The TypeScript notation looks better individually, but imagine if you want to notate a Set of strings, it would beSet<string>which makesstring[]inconsistent and not that clear.I propose we stick with the Facebook Flow notation of always notating the collection type. So it would be
Array<String>andSet<String>. Thoughts?If an Array can contain multiple types. Should we use separate notations
Array<String>Array<Boolean>orArray<Boolean | String>?// @SamVerschueren @jamestalmage @kevva @vdemedes @passy @novemberborn
Beta Was this translation helpful? Give feedback.
All reactions