Using alias when de-structuring records

I wrote this code

type myRecord = {
  id: int,
  name: string
}

let x = {id: 10, name: "test"}
let {aId, aName} = x

Why doesn’t this compile, basically I want to de-structure the record but I want to use alias names rather than the actual name in the record.

You want

let {id: aId, name: aName} = x

Same as in JavaScript, actually.

Unlike tuples, record fields are not positional. You have to state the exact name of the field you want, even if you want to rename it after that.

5 Likes