Skip to content

Default values + destructuring confuses CFA  #12620

Closed
@jwbay

Description

@jwbay

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

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions