Skip to content

Support non-null assertion for destructuring #17390

Closed
@vilicvane

Description

@vilicvane

Currently if we want to retrieve a value nested inside a nullable property using destructuring, we have to split a single destructuring statement into two even if we know that the nullable property would always be non-null in under that branch.

For example:

interface Foo {
  bar?: {
    bia: number;
    pia?: number;
  };
}

let foo: Foo = ...;

// Currently we have to write something like this:
let {bar} = foo;
let {bia, pia} = bar!;

// But ideally we should be able to write something like:
let {
  bar!: {
    bia,
    pia,
  },
} = foo;

// Or even:
let {
  bar!: {
    bia,
    pia!, // <--
  },
} = foo;

Metadata

Metadata

Assignees

No one assigned

    Labels

    SuggestionAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it adds

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions