typescript - TS - how to restore the old --noImplicitAny behaviour -
could tell me how archieve situation, code like
let x;
will throw exception , won't emit it? prefer behaviour ts versions before 2.1. maybe tslint resolve problem?
the difference since 2.1 when assign x
infer actual type rather using any
. before assign number x
may appear has type any
in fact trying use you'll find starts type undefined
.
it doesn't show in one-line example until assign x
can use undefined
, after assigning type widens or changes accordingly.
consider code:
function foo(z: boolean) { let x; if (x) { // editor sees type here `undefined` x.foo(); // error here, type of `x` `never` } x = 3; // `x` has type `number` if (z) { x = 'foo'; // `x` has type `string` } let y = x; // both `x` , `y` have type `string|number` console.log(y.tofixed()); // error, `tofixed` not exist on `string|number` }
change definition let x:any;
, code compile without complaint, because here type of x
any
.
function foo(z: boolean) { let x: any; x = 3; if (z) { x = 'foo'; } let y = x; console.log(y.tofixed()); }
the type of y
explicitly any
because inferred type of x
.
so going 1 liner:
let x;
this not implicit any
because inferred type undefined
.
and answer question, no can't turn off type inferencing.
Comments
Post a Comment