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

Default values + destructuring confuses CFA #12620

Closed
jwbay opened this issue Dec 2, 2016 · 4 comments
Closed

Default values + destructuring confuses CFA #12620

jwbay opened this issue Dec 2, 2016 · 4 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@jwbay
Copy link
Contributor

jwbay commented Dec 2, 2016

TypeScript Version: 2.1.1 and nightly (2.2.0-dev.20161202)

Code

// A *self-contained* demonstration of the problem follows...
let node: {
    props: any;
};

const { props: { className = '' } = {} } = node;

if (className) {
    node.props.className = className.slice(0, 1); // error TS2339: Property 'slice' does not exist on type 'never'.
}

Expected behavior:
Code compiles without error as in TS 2.0.10

Actual behavior:
Error as commented.

Might be duplicate/related to #10065 and its linked issues somehow, but I'm not sure. Strict nullchecks is not enabled.

The following workarounds fix the error:

  1. Change const to let
  2. Use if typeof className === 'string' instead of if (className)
  3. Take out the default values from the destructuring pattern
@mhegazy
Copy link
Contributor

mhegazy commented Dec 2, 2016

className should be any really, and i think this is the issue. we give className the type from the intializer, and that is not correct, it should be typeof initializer | typeof node.props.className

@mhegazy mhegazy added the Bug A bug in TypeScript label Dec 2, 2016
@mhegazy mhegazy added this to the TypeScript 2.2 milestone Dec 2, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Dec 2, 2016

here is an example to demostrate the issue:

let node: {
    props: any;
} = { props: { className: null } };

const { props: { className = '' } = {} } = node;

if (className)  // this should be a valid check, since className can be null.

@manojdcoder
Copy link

Looks like issue I'm facing is similar,

// default value for b can't be a string
function call({a, b = "Xyz"}: { a: string, b?: number }) {
    alert(b);
}

call({
    a:'Hello'
})

call({
    a: 'Hello',
    b: 10
})

@sandersn
Copy link
Member

sandersn commented Oct 4, 2017

Fix is up at #18955.

@sandersn sandersn added the Fixed A PR has been merged for this issue label Oct 4, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants