Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support non-null assertion for destructuring #17390

Closed
vilicvane opened this issue Jul 25, 2017 · 5 comments
Closed

Support non-null assertion for destructuring #17390

vilicvane opened this issue Jul 25, 2017 · 5 comments
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@vilicvane
Copy link
Contributor

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;
@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Jul 25, 2017
@Shlomibo
Copy link

Shlomibo commented Sep 14, 2017

Could be achieved with (or with better typings):

let {
  bar: {
    bia,
    pia = 0,
  } = {} as Record<string, any> // Or Partial<Bar> if we have a Bar type,
} = foo;

What's bothering me, is that we have to have an explicit type assertion on the default value in order to being able to destruct it. It would be better if the default-value type would be inferred to be Partial, or other - destructuring supporting - type.

@RyanCavanaugh RyanCavanaugh added Too Complex An issue which adding support for may be too complex for the value it adds and removed In Discussion Not yet reached consensus labels Oct 30, 2017
@RyanCavanaugh
Copy link
Member

General opinion from the design meeting was that the destructuring syntax is already overly complex and hard to follow, so we'd prefer to not add any new syntax there unless it's absolutely necessary.

@isaiahtaylorhh
Copy link

isaiahtaylorhh commented Feb 20, 2020

Hey guys, it's been a few years on this one. I understand the original opinion that the destructuring syntax is complex. However, I believe the syntax is now hardened and solidified such that it's not going anywhere and is best practice. I think TypeScript should affirm that best practice with type checking. As it is, we resort to much more complex patterns due to the lack of this feature.

interface Office {
  company: {
    name : string;
    industry?: string;
    address?: {
      street: string;
      city: string;
    }
  }
}


// How it should be

function doOffice(office: Office) {
  const { company: { name, industry, address?: { street, city } } } = office;

  ...

  // explicit null checks
  if (!street || !city) {
    //do something
  }
}


// How it is now

function doOffice(office: Office) {
  const { company: { name, industry, address } } = office;

  if (!address) {
    //do something
  }

  const { street, city } = address;
}

As objects grow in complexity, this becomes tedious and very hard to read. Thanks for considering TS team!

@Hulkmaster
Copy link

@RyanCavanaugh sorry for bumping old thread, but i just encountered same situation and i agree with @isaiahtaylorhh , and, guessing from all these upvotes, same goes for plenty of people

also i don't think "becoming too complex" is valid reason not to do it, because that will only mean stagnation of the tool
JS each year becomes "more and more complex", and if you will follow same ideology, then that would mean you will ignore new langauge features

@IARI
Copy link

IARI commented Mar 17, 2023

Please reconsider.
writing something simple and very easily readably as

const { user } = auth

becomes useless, if i have to check it for null, or need an extra line.
imho that defeats the purpose of the most simple use case for destructuring there is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds
Projects
None yet
Development

No branches or pull requests

6 participants