Skip to content

Object destructuring with a default over a source lacking the property reports spurious 'Property does not exist' (declaration + assignment) #796

@nickna

Description

@nickna

Summary

Destructuring an object property that carries a default, over a source whose static type does not declare that property, reports a spurious Property 'x' does not exist on type '...' — even though the default makes the access safe. tsc accepts these (the binding pattern contributes an optional property as contextual type to the source).

const { a = 5 } = {};                       // Type Error: Property 'a' does not exist on type '{ }'.
let b; ({ b = 5 } = {});                     // same (assignment form, #780)
let x; ({ a: x = 5 } = {});                  // same (existing #754 rename form)
({ p: { x } = {} } = { p: { x: 1 } });       // same: inner default `{}` lacks `x` (#779)

A source that declares the property (even optional) works:

const o: { a?: number } = {};
const { a = 5 } = o;                          // OK -> a === 5

Cause

SharpTS does not thread the binding/assignment pattern as a contextual type to the destructuring source. The desugared property read (_src.a, via Expr.Get) is type-checked against the source's declared type; a closed object type ({}, or any type missing the key) has no a, so CheckGet reports the missing-property error. tsc instead types the source under the pattern's implied type, in which a defaulted property is optional, so _src.a reads as T | undefined and the default covers it.

Note the asymmetry with arrays: the desugared array index _src[i] is OOB-tolerant in the checker (no error), so const [a = 5] = [] already works; only the object property path errors. A property without a default correctly stays an error (const { a } = {} is a tsc error too) — the leniency must apply only when a default is present.

Scope

Pre-existing; affects declaration destructuring and the shipped #754 assignment-destructuring forms. Surfaced again while completing #780 (object shorthand-with-default parsing) and #779 (nested pattern with default) — the parser/lowering for those is correct, but their canonical empty-source/empty-default examples hit this type error. Same root cause family as #783 (no binding-pattern contextual typing, there for array-literal tuple inference).

Fix shape

Synthesize the pattern's implied source type (defaulted/optional properties) and type the source under it as a contextual type — or, more narrowly, make a defaulted destructuring property read tolerate a missing property (treat it as propertyType | undefined) while leaving a non-defaulted read strict (matching tsc).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions