#Parameter destructuring and type annotations

21 messages · Page 1 of 1 (latest)

kindred stream
#

Is there any way to destructure arguments and write type annotation without repeating every key?
The following examples is pretty extreme, but it is such a pain to have to write it this way. Not only is it verbose and repetitive but you can easily end up with keys in different order, and the default value assignment becomes disconnected from the type annotation. It just seems like there has to be a better way to write this?

function projectToDataRow(
  project: Project,
  {
    fieldsById,
    customNames,
    nestedCostRowsSources,
    includeProjectTimePeriodData,
    tags,
    formatMessage,
    portfolioBaselinePlanUsers,
    attachments,
    timeFrame,
    weightsByFieldId,
    includeAllocations = false,
    includeTagsMetadata = false,
    includeSpendPlanColumns = false,
    includeDependenciesColumn = false
  }: {
    fieldsById: { [id: string]: Field; },
    customNames: GroupedCustomNames,
    nestedCostRowsSources?: NestedCostRowSources,
    includeProjectTimePeriodData: boolean,
    includeAllocations: boolean,
    tags?: Tag[],
    formatMessage?: FormatMessage,
    portfolioBaselinePlanUsers?: PortfolioPlanUser[],
    includeTagsMetadata: boolean,
    attachments?: Attachment[],
    timeFrame?: TimeFrame,
    includeSpendPlanColumns: boolean,
    includeDependenciesColumn: boolean,
    weightsByFieldId?: Record<string, FieldWeight>
  }
) {

}
#

I wish I could do something like this:

function projectToDataRow(
  project: Project,
  {
    fieldsById: Record<string, Field>,
    customNames: GroupedCustomNames,
    nestedCostRowsSources?: NestedCostRowSources,
    includeProjectTimePeriodData: boolean = true,
    tags?: Tag[],
    formatMessage: FormatMessage,
    portfolioBaselinePlanUsers: PlanUser[],
    attachments?: Attachment[],
    timeFrame?: TimeFrame,
    weightsByFieldId?: Record<string, Weight>,
    includeAllocations: boolean = false,
    includeTagsMetadata: boolean = false,
    includeSpendPlanColumns: boolean = false,
    includeDependenciesColumn: boolean = false
  }
) { }

I realize this syntax conflicts with nested destructuring... but any syntax that avoided the repetition of every single key would be amazing

rotund cradle
#

there is no such simplified syntax, and it's challenging to come up with a syntax that supports type declarations, aliasing, and defaults all at the same time

rotund cradle
#

not to mention something like

const { foo: { a: string, b: number } } = { foo: { a: 1, b: 2 } };
#

is perfectly legal javascript

#

it creates two variables named string and number

#

and sets string = 1 and number = 2

#

worse

const { foo: { a: string, b: number = 2 } = { a: 1, b: 2 } } = { foo: { a: 1, b: 2 } };

is also legal

#

it's unclear how you could extend the syntax in a sane way to include the type declaration

blazing barn
#

imagine ::

rotund cradle
#

well, yes, but that's pretty ugly too, but I'm hoping to get the OP to consider the problem

#

we don't have it and probably aren't getting it any time soon

blazing barn
#

key:[rename]:[typedecl][=default]

#

sorry, intrusive thoughts won

void wind
#

like opts or x

#

alternatively extract the type definition into an interface

#

you'd still be repeating every key but at least consumers can do : TheInterface, not that they'll need to...

rotund cradle
void wind
#

wait what