Skip to content

Unsafe type mismatch of destructuring assignment #6125

Closed
@falsandtru

Description

@falsandtru

A following valid destructuring assignment always occurs a TypeError.

// typescript
var {x: {y}}: {x?: {y?}} = {}; // should be a compile error
// emit
var y = {}.x.y; // TypeError: (intermediate value).x is undefined
// javascript on firefox
var {x:{y}} = {}; // TypeError: can't convert undefined to object

Destructuring assignment should constrain type declarations.

// expects
var {x: {y}}: {x: {y}} = {x: {y: 0}}; // ok
var {x: {y}}: {x?: {y?}} = {}; // error
var {x: {y = 0}}: {x?: {y?}} = {}; // error
var {x: {y} = {y: 0}}: {x?: {y?}} = {}; // error
var {x: {y = 0} = {}}: {x?: {y?}} = {}; // error
var {x: {y = 0} = {y: 0}}: {x?: {y?}} = {}; // ok

Destructuring assignments violate a following type constraint.

var options: {x?: {y?}};
var required: {x: {y}} = options; // compile error, but destructuring assignments are not.
var {x: {y}}: {x?: {y?}} = {}; // should be a compile error

Destructuring assignments have a own implicit type but TypeScript breaks it type improperly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions