You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Default values + destructuring confuses CFA #12620
TypeScript Version: 2.1.1 and nightly (2.2.0-dev.20161202)
Code
// A *self-contained* demonstration of the problem follows...letnode: {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:
Change const to let
Use if typeof className === 'string' instead of if (className)
Take out the default values from the destructuring pattern
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
letnode: {props: any;}={props: {className: null}};const{props: { className =''}={}}=node;if(className)// this should be a valid check, since className can be null.
// 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
})
Activity
mhegazy commentedon Dec 2, 2016
classNameshould beanyreally, and i think this is the issue. we giveclassNamethe type from the intializer, and that is not correct, it should betypeof initializer | typeof node.props.classNamemhegazy commentedon Dec 2, 2016
here is an example to demostrate the issue:
manojdcoder commentedon Dec 8, 2016
Looks like issue I'm facing is similar,
sandersn commentedon Oct 4, 2017
Fix is up at #18955.