Skip to content

Different behavior when destructuring from using dot notation. #35443

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

Closed
larsbs opened this issue Dec 2, 2019 · 2 comments
Closed

Different behavior when destructuring from using dot notation. #35443

larsbs opened this issue Dec 2, 2019 · 2 comments

Comments

@larsbs
Copy link

larsbs commented Dec 2, 2019

TypeScript Version: 3.7.2

Search Terms: different behavior when destructuring typescript strictNullChecks

Code

interface Data {
  readonly name: string;
  readonly id: number;
}

interface MainInputLoading {
  readonly isLoading: true;
  readonly data: null;
}

interface MainInputLoaded {
  readonly isLoading: false;
  readonly data: Data;
}

type MainInput = MainInputLoading | MainInputLoaded;

function main({ isLoading, data }: MainInput) {
  if (isLoading) {
    return;
  }

  console.log(data.id);  // Fails
}

function altMain(input: MainInput) {
  if (input.isLoading) {
    return;
  }

  console.log(input.data.id);  // Doesn't fail
}

Expected behavior: They should behave exactly the same as TS should be able to know that the destructured props still refer to the original object.

Actual behavior: In one case we have an error and in the other we don't.

Playground Link: https://codesandbox.io/s/sweet-williamson-7bs9d

Related Issues: No

@IllusionMH
Copy link
Contributor

Looks like duplicate of #33097, #28599, and many other which in the end are related to #12184

@larsbs
Copy link
Author

larsbs commented Dec 2, 2019

Duplicated then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants