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

Destructing doesnt allow for discriminated union #41644

Closed
maraisr opened this issue Nov 23, 2020 · 3 comments
Closed

Destructing doesnt allow for discriminated union #41644

maraisr opened this issue Nov 23, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@maraisr
Copy link
Member

maraisr commented Nov 23, 2020

TypeScript Version: 4.1.0-beta

Code

function generate(who: string): {json: true, body: {hello: string}} | {json: false, body: string} {
    return {json: true, body: {hello: who}}
}

const {body, json} = generate('world');

if (json) {
    body // should give you auto-complete, for `.hello`
} else {
    body // should be a string, thus giving `includes` as an example
}

Issue
As you'll find from the code example, body on line 8 doesnt autocomplete with .hello

Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

@MartinJohns
Copy link
Contributor

Duplicate of #12184.

@maraisr
Copy link
Member Author

maraisr commented Nov 23, 2020

Sometimes issues are academic speak that I have no clue if it's even the same thing! Thanks for the "duplicate" advice, I shall subscribe to that issue. Maintainers feel free to close this perhaps.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Nov 25, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants