Fixes #126 by implementing a long-sought syntax for typing individual object properties and array elements in left-hand-side patterns (including function arguments and assignment). Basically, :: type can come at the end of any existing object property or array element. For example:
function Component({
name: [first:: string, last:: string, ...rest:: string[]],
counter:: number
setCounter: sc:: (number) => void
})
↓↓↓
function Component({
name: [first, last, ...rest],
counter,
setCounter: sc
}: {
name: [ string, string,... string[],],
counter: number,
setCounter: (number) => void,}){}
Why this notation?
name:: type: value is ambiguous: it could be interpreted as name:: {type: value} (typing as object). (I tried to include this as an option at first, but ran into this alternate parse.)
name: value: type is similarly ambiguous: it could be interpreted as name: {value: type} (complex destructuring).
I admit I still often type the wrong number of :s when writing examples, but I imagine we'll get used to it. (Also open to better proposals for notation.)
I still need to write actual docs, but hopefully the example above and the tests give a reasonable idea.
Fixes #126 by implementing a long-sought syntax for typing individual object properties and array elements in left-hand-side patterns (including function arguments and assignment). Basically,
:: type
can come at the end of any existing object property or array element. For example:Why this notation?
name:: type: value
is ambiguous: it could be interpreted asname:: {type: value}
(typing as object). (I tried to include this as an option at first, but ran into this alternate parse.)name: value: type
is similarly ambiguous: it could be interpreted asname: {value: type}
(complex destructuring).I admit I still often type the wrong number of
:
s when writing examples, but I imagine we'll get used to it. (Also open to better proposals for notation.)I still need to write actual docs, but hopefully the example above and the tests give a reasonable idea.