DanielXMoore / Civet

A TypeScript superset that favors more types and less typing
https://civet.dev
MIT License
1.55k stars 33 forks source link

Typing of object/array pattern components via `::` #1383

Closed edemaine closed 2 months ago

edemaine commented 2 months ago

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?

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.