Flow: Shorten syntax for multiple continuous params with same type

Created on 23 Mar 2018  路  4Comments  路  Source: facebook/flow

Like in Go you can use:

func add(x, y int) int {
    return x + y
}

func main() {
    fmt.Println(add(42, 13))
}

This is really concise and readable at the same time, and helps to shorten the function declaration.
For example:

function store(file, section, attribute, field: string, value: any) {
}
// instead of
function store(file: string, section: string, attribute: string, field: string, value: any) {
}
feature request

Most helpful comment

This is really too specific case to introduce special syntax for it.

All 4 comments

This is really too specific case to introduce special syntax for it.

This syntax is already allowed and means a different thing.

Even if it was possible, I find readability of this construct is pretty bad. If there are more than one or two parameters I instead use multiple lines, IMO much more readable:

function store(
    file: string,
    section: string,
    attribute: string,
    field: string,
    value: any
): void {
    // ....
}

Alternatively, when there are more than 3 or 4 parameters, I may use an object and create a type for that object, then the parameter type is just the name of that object. I add a JSDoc for the type to get inline popup info/help for the type from the IDE.

/**
 * Some text
 * @typedef {Object} ParamsType
 * @param {string} file Some explanation
 * @param {string} section
 * @param {string} attribute
 * @param {string} field
 * @param {*} value
 */
type ParamsType = {|
    file: string,
    section: string,
    attribute: string,
    field: string,
    value: any
|};

/**
 * As short as it gets
 * @param {ParamsType} o
 */
function store(o: ParamsType): void {
    // ....
}

This syntax means something else in flow already:

func f(x, y: int): int

namely, infer the first argument and require the second to be an int. If anyone feels like proposing alternative syntax, feel free to comment or open another issue with said proposal.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

l2silver picture l2silver  路  3Comments

cubika picture cubika  路  3Comments

ctrlplusb picture ctrlplusb  路  3Comments

damncabbage picture damncabbage  路  3Comments

mmollaverdi picture mmollaverdi  路  3Comments