Skip to content

inference strangeness - 3 #6037

Closed
Closed
@zpdDG4gta8XKpMCd

Description

@zpdDG4gta8XKpMCd

Overheard conversation on the street:

Q: Can I assign an array literal to a tuple in TypeScript?
A: Yes, sometimes you can.

let value : [string, string];
value = ['hey', 'nay']; // <-- works, an array literal can be assigned to a tuple  

function fold<a, r>(values: a[], result: r, fold: (result: r, value: a) => r) : r {
    for (let value of values) {
        result = fold(result, value);
    }
    return result;
}

function append<a>(values: a[], value: a) : a[] {
    values.push(value);
    return values;
}


fold(
    [1, 2, 3],
    <[string, string][]>[],
    (result, value) => append( // <-- doesn't work, an array literal is not assignable to tuple 
        result,
        ['', '']
    )
);

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions